RSS feed
  • python gtk close on escape key

    Posted on February 17th, 2009 1 comment

    I wanted a gtk window to close when the escape key is pressed:

     
    import pygtk
    pygtk.require('2.0')
    import gtk
     
    class CloseOnEscape :
    	def keypress(self, widget, event) :
    		if event.keyval == gtk.keysyms.Escape :
    			gtk.main_quit()
     
    	def __init__(self):
    		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    		self.window.connect("key-press-event", self.keypress)
    		self.window.show()
     
    	def main(self):
    		gtk.main()
     
    if __name__ == "__main__":
    	app = CloseOnEscape()
    	app.main()
     

One Response to “python gtk close on escape key”

  1. Cool Thanks for your post. I am just starting python and this was a big help.

Leave a Reply