右键单击扩展器中的PyGTK Hbox [英] Right-Click on a PyGTK Hbox in an Expander

查看:185
本文介绍了右键单击扩展器中的PyGTK Hbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  def launchBrowser(widget,host,event):
print event
打印主机
如果event.type == gtk.gdk.BUTTON_PRESS:
如果event.button == 3:
webbrowser.open(主机,1)
打印

def addServer(self,loginfo):
main_expand = gtk.Expander()
main_led = gtk.Image()
如果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拦截。



Anyhelp是welcome =)
提前感谢!



编辑:
我也尝试过:

  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_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:
如果event.button == 3:
打印点击

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

如果__name__ =='__main__':
main()


解决方案

代码中的细微变化。



在Expander小部件而不是Label小部件上添加button-press-event

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

class MainWindow(gtk.Window):
def __init __(self):
gtk.Window $ _ $($)
self.set_default_size(300,300)
self.addServer()

def launchBrowser(self,widget,event,host,* args):
如果event.type == gtk.gdk.BUTTON_PRESS:
如果event.button == 3:
打印点击
#单击Click扩展器的正常行为
expand = widget.get_expanded()
如果不展开:widget.set_expanded(True)
else:widget.set_expanded(False)

def addServer(self):
main_expand = gtk.Ex pander()
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()

如果__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()

解决方案

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

这篇关于右键单击扩展器中的PyGTK Hbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆