单击事件的菜单项在 gtk3 + python3 中不起作用
Menu Item on click event not working in gtk3 + python3
我在 python3 和 Gtk3 中编写了一个脚本,我希望它在我单击 "close" 菜单项时关闭 window,但是当我什么都不做时附加。我正在使用 Ubuntu 16.10.
这是脚本:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MenuTest(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title='MenuTest')
Box=Gtk.VBox()
self.add(Box)
Menubar=Gtk.MenuBar()
Menu1=Gtk.Menu()
Mfile=Gtk.MenuItem("_File")
Mfile.set_submenu(Menu1)
Clos=Gtk.MenuItem("Close")
Menu1.append(Clos)
Clos.connect('button-press-event', Gtk.main_quit)
Menubar.append(Mfile)
Box.pack_start(Menubar, expand=True, fill=True, padding=0)
Wind=MenuTest()
Wind.connect('delete-event', Gtk.main_quit)
Wind.show_all()
Gtk.main()
我哪里做错了?
您需要使用信号 "activate" 而不是 "button-press-event"。
"button-press-event"
信号来自 Gtk.Widget
class,Gtk.MenuItem
是从中导出的。当按下通常来自鼠标的按钮时,会发出此信号。需要为小部件设置事件掩码以接收此信号,因为此信号为 Gdk.EventMask.BUTTON_PRESS_MASK
。通常不需要这个信号。
Gtk.MenuItem
有 "activate signal"
,它在激活项目时发出。这可能是通过鼠标按钮以及键盘、触摸屏等...
我在 python3 和 Gtk3 中编写了一个脚本,我希望它在我单击 "close" 菜单项时关闭 window,但是当我什么都不做时附加。我正在使用 Ubuntu 16.10.
这是脚本:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MenuTest(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title='MenuTest')
Box=Gtk.VBox()
self.add(Box)
Menubar=Gtk.MenuBar()
Menu1=Gtk.Menu()
Mfile=Gtk.MenuItem("_File")
Mfile.set_submenu(Menu1)
Clos=Gtk.MenuItem("Close")
Menu1.append(Clos)
Clos.connect('button-press-event', Gtk.main_quit)
Menubar.append(Mfile)
Box.pack_start(Menubar, expand=True, fill=True, padding=0)
Wind=MenuTest()
Wind.connect('delete-event', Gtk.main_quit)
Wind.show_all()
Gtk.main()
我哪里做错了?
您需要使用信号 "activate" 而不是 "button-press-event"。
"button-press-event"
信号来自 Gtk.Widget
class,Gtk.MenuItem
是从中导出的。当按下通常来自鼠标的按钮时,会发出此信号。需要为小部件设置事件掩码以接收此信号,因为此信号为 Gdk.EventMask.BUTTON_PRESS_MASK
。通常不需要这个信号。
Gtk.MenuItem
有 "activate signal"
,它在激活项目时发出。这可能是通过鼠标按钮以及键盘、触摸屏等...