1

我有一个 gtk.Expander 对象,它的标签中包含一个 gtk.HBox,它包含一个 gtk.Image 和一个 gtk.Label。我想在右键单击 HBox 时启动 Webbrowser。这是我的代码:

def launchBrowser(widget, host, event):
    print event
    print host
    if event.type == gtk.gdk.BUTTON_PRESS:
        if event.button == 3:
            webbrowser.open(host, 1)
            print "right click"

def addServer(self, loginfo):
    main_expand = gtk.Expander()
    main_led = gtk.Image()
    if int(loginfo["main"]) == 0:
        main_led.set_from_stock(gtk.STOCK_STOP, gtk.ICON_SIZE_BUTTON)
    else:
        main_led.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_BUTTON)

    main_srvname = gtk.Label(loginfo["srvname"])

    expand_title = gtk.HBox(False, 2)
    expand_title.pack_start(main_led, False, True, 0)
    expand_title.pack_start(main_srvname, True, True, 0)

    main_srvname.add_events(gtk.gdk.BUTTON_PRESS_MASK)
    main_srvname.connect_object('event', self.launchBrowser, loginfo["host"])
    main_srvname.emit('event', gtk.gdk.Event(gtk.gdk.BUTTON_PRESS_MASK))

    main_expand.set_property("label-widget", expand_title)

问题是当我在这个 HBox 上传递我的光标时,我正确地收到了事件。但是当我点击什么都没有发生。我认为这是因为单击事件被扩展器拦截。

欢迎任何帮助=)提前致谢!

编辑:我也试过了:

    main_srvname = gtk.Label(loginfo["srvname"])
    eventbox = gtk.EventBox()
    eventbox.add(main_srvname)
    eventbox.connect_object('button-press-event', self.launchBrowser, loginfo["host"])

    # Titre de l'expand box 
    expand_title = gtk.HBox(False, 2)
    expand_title.pack_start(main_led, False, True, 0)
    expand_title.pack_start(eventbox, True, True, 0)

也不工作...

EDIT2:根据 Jeremy 的要求,这里有一个独立的代码,只需复制粘贴它你这个懒惰的男孩!

import pygtk
pygtk.require('2.0')
import gtk

class MainWindow(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        self.set_default_size(300, 300)
        self.addServer()

    def launchBrowser(widget, host, event):
        print event
        if event.type == gtk.gdk.BUTTON_PRESS:
            if event.button == 3:
                print "click"

    def addServer(self):
        main_expand = gtk.Expander()
        main_led = gtk.Image()
        main_led.set_from_stock(gtk.STOCK_STOP, gtk.ICON_SIZE_BUTTON)

        main_srvname = gtk.Label("srvname")
        main_srvname.add_events(gtk.gdk.BUTTON_PRESS_MASK)
        main_srvname.connect_object('button-press-event', self.launchBrowser, "host")

        expand_title = gtk.HBox(False, 2)
        expand_title.pack_start(main_led, False, True, 0)
        expand_title.pack_start(main_srvname, True, True, 0)
        main_expand.set_property("label-widget", expand_title)

        self.add(main_expand)
        self.show_all()

def main():
    MainWindow()
    gtk.main()

if __name__ == '__main__':
    main()
4

1 回答 1

-1

代码中的小改动。

在 Expander 小部件而不是 Label 小部件上添加“按钮按下事件”

import pygtk
pygtk.require('2.0')
import gtk

class MainWindow(gtk.Window):
   def __init__(self):
     gtk.Window.__init__(self)
     self.set_default_size(300, 300)
     self.addServer()

   def launchBrowser(self, widget, event, host, *args):
      if event.type == gtk.gdk.BUTTON_PRESS:
        if event.button == 3:
            print "click"
     # Normal behaviour of Expander on single click
      expand = widget.get_expanded()
      if not expand: widget.set_expanded(True)
      else: widget.set_expanded(False)

  def addServer(self):
    main_expand = gtk.Expander()
    main_led = gtk.Image()
    main_led.set_from_stock(gtk.STOCK_STOP, gtk.ICON_SIZE_BUTTON)

    main_srvname = gtk.Label("srvname")
    main_expand.add_events(gtk.gdk.BUTTON_PRESS_MASK)
    main_expand.connect('button-press-event', self.launchBrowser, 'host')

    expand_title = gtk.HBox(False, 2)
    expand_title.pack_start(main_led, False, True, 0)
    expand_title.pack_start(main_srvname, True, True, 0)
    main_expand.set_property("label-widget", expand_title)

    self.add(main_expand)
    self.show_all()

def main():
 MainWindow()
 gtk.main()

if __name__ == '__main__':
 main()
于 2011-08-24T08:57:46.917 回答