GTK弹出菜单未出现在鼠标位置

2024-09-28 21:53:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个Gtk弹出菜单按钮点击使用如下所示:

def code_clicked(self,widget,event):
    newmenu=Gtk.Menu()
    newitem=Gtk.MenuItem('hello')
    newmenu.append(newitem)
    newitem1=Gtk.MenuItem('goodbye')
    newmenu.append(newitem1)
    newmenu.show_all()
    newmenu.popup(None,None,None,None,event.button,event.time)
    return True

菜单永远不会出现。理论上,popup中的第三个参数func在设置为Null时将位置设置为光标位置。我认为问题是因为如果我将func设置为lambda x,y: (event.x,event.y,True),它会在光标上方约100像素处显示弹出菜单。在

我想找个方法在光标处弹出这个菜单。任何帮助都将不胜感激!在


Tags: noneeventtruegtkdef菜单按钮func
2条回答

您将event.time作为数据参数传递给func以确定菜单位置。将您的呼叫更改为以下内容应该可以解决此问题:

newmenu.popup(None, None, None, event.button, event.time)

你需要窗口的根位置。 所以

        menu.popup(None, None, 
                   lambda menu, data: (event.get_root_coords()[0],
                                       event.get_root_coords()[1], True),
                   None, event.button, event.time)

应该把弹出窗口放在正确的位置。在这里工作。在

至于Jon Black的答案,在GTK3中,问题是正确的,请参见http://developer.gnome.org/gtk3/3.4/GtkMenu.html#gtk-menu-popup不推荐的pygtk绑定只会像您描述的那样工作

相关问题 更多 >