右键单击扩展器中的 PyGTK Hbox

发布于 2024-12-01 11:25:54 字数 2802 浏览 0 评论 0原文

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

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 上时,我正确地收到了该事件。但当我点击时什么也没有发生。我认为这是因为点击事件被Expander拦截了。

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

编辑: 我也尝试过:

    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)

也不工作...

编辑2: 按照 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()

I've got a gtk.Expander object, containing in its label a gtk.HBox, which packed a gtk.Image and a gtk.Label.
I want to launch a Webbrowser when the HBox is right-clicked.
Here is my code:

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)

Problem is that when I pass my cursor on this HBox, I correctly received the event. But when I clicked nothing happen. I think it's because the click event is intercept by the Expander.

Anyhelp is welcome =)
Thanks in advance!

EDIT:
I also tried that:

    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)

Not working either...

EDIT2:
As requested by Jeremy, here a self-contained code, just copy-paste it you lazyboy!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2024-12-08 11:25:54

对您的代码进行小改动。

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

Small change in your code.

Add "button-press-event" on Expander widget instead of Label widget

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文