开发者

Catching a click anywhere inside a gtk.Window

开发者 https://www.devze.com 2023-01-03 21:11 出处:网络
consider the following python code: import gtk class MainWindow(): def _开发者_Go百科_init__(self):

consider the following python code:

import gtk

class MainWindow():
    def _开发者_Go百科_init__(self):
        self.window = gtk.Window()
        self.window.show()

if __name__ == "__main__":
    main = MainWindow()
    gtk.main()

I'd need to catch clicks anywhere inside this gtk.Window(). I haven't found any suitable event (I also tried button-press-event, but it doesn't work), what am I missing?

Thank you!


You can pack a gtk.EventBox into the window. In general, whenever you have troubles catching events, check if gtk.EventBox solves them.

import gtk

class MainWindow():
    def __init__(self):
        self.window = gtk.Window()
        self.box = gtk.EventBox ()
        self.window.add (self.box)
        self.box.add (gtk.Label ('some text'))
        self.window.show_all()

        import sys
        self.box.connect ('button-press-event',
                          lambda widget, event:
                              sys.stdout.write ('%s // %s\n' % (widget, event)))

if __name__ == "__main__":
    main = MainWindow()
    gtk.main()

Note, however, that event propagation upwards the widget hierarchy will stop if a widget handles event itself. For instance, a parent of gtk.Button won't receive click events from it.


So i have this DrawingArea in Window. And on click i get the callback

  self.drawingarea = gtk.DrawingArea()
  self.drawingarea.connect ('button-press-event',self.callback)
  self.drawingarea.set_events(gtk.gdk.EXPOSURE_MASK 
                            | gtk.gdk.LEAVE_NOTIFY_MASK 
                            | gtk.gdk.BUTTON_PRESS_MASK 
                            | gtk.gdk.POINTER_MOTION_MASK 
                            | gtk.gdk.POINTER_MOTION_HINT_MASK )
  self.window.add(self.drawingarea)

Filter the left or right button:

  def callback(self, widget, event):
    print "clicking... left or right"
    if event.button == 1:
      print 'OK - clicked left '
      #os.system("""wmctrl -s 0""")
    return True
0

精彩评论

暂无评论...
验证码 换一张
取 消