2010-06-30

Recently, while writing code for heybuddy, I needed to:

  • display a tray icon
  • hide the window when "close window" is selected
  • show the window when the tray icon is clicked

Although getting this done was easy enough to do, finding out how to do it was a bit of a pain, so I thought it would be best if I shared my Python/GTK test code and save future developers from some pain.

Enter the Python

#!/usr/bin/env python
import gtk
class Win (gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        self.set_title("Close to tray")
        self.connect("delete-event"self.delete_event)
        #create a virtical box to hold some widgets
        vbox gtk.VBox(False)
        #make a quit button
        quitbutton gtk.Button("Quit")
        quitbutton.connect("clicked",self.quit)
        #add the quitbutton to the vbox
        vbox.pack_start(quitbutton)
        #create a label to say *something* and add to the vbox
        label=gtk.Label("This is a test of 'close to tray' functionality")
        vbox.pack_start(label)
        #add the vbox to the Window
        self.add(vbox)
        #show all of the stuff
        self.show_all()
        #make a status icon
        self.statusicon gtk.status_icon_new_from_stock(gtk.STOCK_GOTO_TOP)
        self.statusicon.connect('activate'self.status_clicked )
        self.statusicon.set_tooltip("the window is visible")
        #start the gtk main loop
        gtk.main()
    
    def quit(self,button):
        #quit the gtk main loop
        gtk.main_quit()
    
    def delete_event(self,window,event):
        #don't delete; hide instead
        self.hide_on_delete()
        self.statusicon.set_tooltip("the window is hidden")
        return True
        
    def status_clicked(self,status):
        #unhide the window
        self.show_all()
        self.statusicon.set_tooltip("the window is visible")
        
if __name__=="__main__":
    win Win()

The basics of what needs to be done:

  1. connect the Window's "delete-event" signal to a function
  2. in the function, have the window "hide_on_delete()"
  3. return True ( if True is not returned, the Window's children will be destroyed and everything will go to crap )

So there you have it, an easy way to "close to tray" or whatever you want to call it.

Now quit reading, and go ease a developer's pain.

Comments
2010-08-17 cs.ati:
Thank U! This is exactly what I need. U saved me a lot of time. Thanks again!
2010-08-17 jezra:
You are very welcome.
2010-12-09 Guilherme:
THANKS!!! That's just awesome! I thought I would spent a lot of time doing this, but then I found you and now 'm really relieved.
2016-01-25 nurul irfan:
it works, even in PyGObject. the return true is the most important. thanks.
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
2 plus 3
Answer:
required
subscribe
 
2019
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008