Tuesday, December 4, 2012

A Hack for Panel Applet programming on Ubuntu using Python Tkinter

During this sprint at office, I had to program a Panel Applet for the Linux based operating system we use which is based on Ubuntu kernal. The purpose was to display the total time a user is connected to the VPN at the office when they work from home by remotely login into a PC at office. The problem was that this application was going to be integrated to a light weight operating system of Ubuntu (LXDE) which does not support gnome or much graphic programming. (For confidentiality of my work at office, I'm not going to post the actual application here but a similar application for those who are interested. I will show how to develop a internet connection timer which displays the total time connected to internet) 

I searched for a solution in google for hours but finally ended up with empty hands. I found several ways to get it done using GTK+ but with the limited time I got for the development of this application did not allow me wasting time on continuing the search. 

For those who do not know what a Panel Applet is, it's and icon or an application with some functionality that is displayed on the taskbar panel. In Windows terms, it's simply a taskbar application. You can see several applets in the image below... eg: Volume Controller Applet.



First I thought of developing a Python widget using Tkinter GUI libraray to have a working code 
in my hand. It basically had three labels where the first one displays an image (green color for live 
connectivity and red color if disconnected), second one displays the text 'Internet Timer' 
and the third one displays the time connected to internet.
Functionality wise it was perfectly working but the real requirement was to develop it as a panel applet. There was no way I could convert it to a panel applet. The title bar and the control buttons on top of the application were unnecessary because it should not allow the user to close the application. With a single line of python code I could remove the window manager of the application I wrote. Here the root is the Tkinter Frame. root.overrideredirect(1) After removing the window manager, it was displayed on top left corner of the screen on top of all other windows. Then I thought if I could add functionality to drag and move the application on screen it would not block viewing other windows as I could drag and place it in a suitable location.
For labels in the application, I wrote three methods one to start move, another to stop move and another to drag the label. I restricted changing the Y coordinate changing so that user cannot drag it upwards or downwards. User could only drag it towards left or right direction. Then I set the Y coordinate appropriately so that the application appears on top of the taskbar. I changed the color of the taskbar to match the background color of the application so that users cannot identify whether this is not a panel applet.




self.label.bind('<ButtonPress-1>', self.StartMove)

self.label.bind('<ButtonRelease-1>', self.StopMove)
self.label.bind('<B1-Motion>', self.OnMotion)

    def StartMove(self, event):

        self.x = event.x

        self.y = event.y

    def StopMove(self, event):

        self.x = None

        self.y = None

    def OnMotion(self,event):

        x = (event.x_root - self.x - self.label.winfo_rootx() + self.label.winfo_rootx())
        y = root.winfo_screenheight() - 24;
        root.geometry("+%s+%s" % (x, y))

Finally my effort was successful where the application is pasted on the taskbar and can be dragged towards left or right direction within the taskbar. Although it was a cheat, I could get the work done :D

Download the source code here.


Here are some important commands for programming similar applications. 



To install the Tkinter library on ubuntu, 
 
sudo apt-get install python python-tk idle python-pmw python-imaging
To install python-imaging to use images in python applications,  
sudo apt-get install python-imaging
To install imaging library for Tkinter, 

sudo apt-get install python-imaging-tk
Written by : Tharindu Edirisinghe

Monday, November 12, 2012

How to Lock your Ubuntu computer using a Keyboard Shortcut

Here at Virtusa, I am using the Virtusa Operating System as the operating system for my PC. It is built on top of Ubuntu Kernal and therefore whatever the command that works on Ubuntu should work on that also unless it is specifically blocked by its development team. I had been a Windows user for a long time and therefore I had the habit of locking the computer using the keyboard shortcut Windows Key + L . Ubuntu does not have this keyboard shortcut inbuilt and therefore I created a custom keyboard shortcut to get it done.

First I searched in google for the command to lock the screen using the terminal. I found the following command and tried it in the terminal and it locked the screen.



   gnome-screensaver-command -l

Next thing was to execute that command as my own custom command. I created a new file with the name lockmyscreen, wrote the following shell script and saved.


    #! /bin/bash

  echo `gnome-screensaver-command -l`

Then I added the execute permission to the shell script I wrote.



   chmod +x lockmyscreen

I executed the script and it locked my screen.



   ./lockmyscreen

Next thing was to convert the shell script to a command. For that, I moved the script to the location /usr/bin/ where the commands contain. For that I needed root access.



   sudo mv lockmyscreen /usr/bin/lockmyscreen

I provided the root password when it prompted. Then I could run the script I wrote as a command in the terminal.



   lockmyscreen

It was successfully locking my screen. Next thing was to assign a keyboard shortcut to that command. I opened System -> Preferences -> Keyboard Shortcuts as shown in the image below.


Then it opened the following window and I pressed the button Add to add a new keyboard shortcut.


I gave a name for the command and typed lockmyscreen which was the name of the script I wrote in the Command text box. Then pressed the button Apply.


Then it listed down the newly added keyboard shortcut under the Custom Shortcuts list. The shortcut was Disabled by default as I had not assigned a shortcut yet.

I clicked on the text Disabled to add new shortcut. Then the text Disabled changed as New shortcut...
 

Then I pressed the windows key and while holding it I pressed the key L also. Then it added the shortcut and so I closed the window by clicking on the Close button.
After that I could lock my screen as I used to do on windows by pressing the keyboard shortcut Windows Key + L.

Following the same approach, you can write your own commands and assign keyboard shortcuts to them to perform your daily tasks easily.
Feel free to send me your comments.

Written by : Tharindu Edirisinghe