Capturing a click anywhere inside gtk.Window

consider the following python code:

import gtk

class MainWindow():
    def __init__(self):
        self.window = gtk.Window()
        self.window.show()

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

I need to catch clicks anywhere in this gtk.Window (). I did not find a suitable event (I also tried button-press-event, but it does not work), what am I missing?

Thank!

+3
source share
2 answers

You can pack by gtk.EventBoxwindow. In general, whenever you have problems with traps, check to see if they resolve gtk.EventBox.

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()

, , , , . , gtk.Button .

+4

, DrawingArea .

  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)

:

  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

Source: https://habr.com/ru/post/1750837/


All Articles