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

No comments:

Post a Comment