wxpython弹出菜单事件处理程序不起作用

时间:2018-11-24 15:31:05

标签: events callback menu wxpython handler

所以,我有一些简单的框架代码,请注意wx.Frame是 not 的子类,并且框架在全局范围内运行:

import wx

app = wx.App()

def OnSettings(event):
    print ("'Settings...' selected")

def OnDefaults(event):
    print ("'Defaults...' selected")

def OnPrefs(event):
    print ("'Preferences' selected")

def OnOpen(event):
    print ("'Open...' selected")

def OnClose(event):
    print ("'Close' selected")

def OnCloseAll(event):
    print ("'CloseAll' selected")

def OnNew(event):
    print ("'New...' selected")

def OnQuit(event):
    print ("Quit selected")
    top.Close(True)

def OnExit(event):
    dlg = wx.MessageDialog(top, "Do you really want to close this application?", "Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
    result = dlg.ShowModal()
    dlg.Destroy()
    if result == wx.ID_OK:
       top.Destroy()
    top.Close(True)

def OnRightDown(event):
    top.PopupMenu(rootMenu, event.GetPosition())

rootMenu = wx.Menu()
fileMenu = wx.Menu()

itemNew = wx.MenuItem(fileMenu, wx.NewId(), "New...","Create a new instance of something")
fileMenu.Bind(wx.EVT_MENU, OnNew, itemNew)
fileMenu.Append(itemNew)

itemOpen = wx.MenuItem(fileMenu, wx.NewId(), "Open...","Open an existing instance of something")
fileMenu.Bind(wx.EVT_MENU, OnOpen, itemOpen)
fileMenu.Append(itemOpen)

fileMenu.AppendSeparator()

itemClose = wx.MenuItem(fileMenu, wx.NewId(), "Close","Close whatever is open on top")
fileMenu.Bind(wx.EVT_MENU, OnClose, itemClose)
fileMenu.Append(itemClose)

itemCloseAll = wx.MenuItem(fileMenu, wx.NewId(), "Close All","Close everything")
fileMenu.Bind(wx.EVT_MENU, OnCloseAll, itemCloseAll)
fileMenu.Append(itemCloseAll)

rootMenu.AppendSubMenu(fileMenu,"Manage Data","Open, close, or create resources")

configMenu = wx.Menu()

itemSettings = wx.MenuItem(configMenu, wx.NewId(), "Settings...","Configure environmental operating parameters")
configMenu.Bind(wx.EVT_MENU, OnSettings, itemSettings)
configMenu.Append(itemSettings)

itemDefaults = wx.MenuItem(configMenu,wx.NewId(), "Defaults...","Configure default data sources, views, and processing options")
configMenu.Bind(wx.EVT_MENU, OnDefaults, itemDefaults)
configMenu.Append(itemDefaults)

itemPrefs = wx.MenuItem(configMenu,wx.NewId(), "Preferences...","Configure User Interface Cosmetics")
configMenu.Bind(wx.EVT_MENU, OnPrefs, itemPrefs)
configMenu.Append(itemPrefs)

rootMenu.AppendSubMenu(configMenu,"Manage Configuration","Configure Application for operations")

terminalMenu = wx.Menu()

itemExit = wx.MenuItem(terminalMenu,wx.NewId(), "Exit","Terminate after updating and closing anything that's open.")
terminalMenu.Bind(wx.EVT_MENU, OnExit, itemExit)
terminalMenu.Append(itemExit)

itemQuit = wx.MenuItem(terminalMenu,wx.NewId(), "Quit","Close the root window and abandon anything unsaved.")
terminalMenu.Bind(wx.EVT_MENU, OnQuit, itemQuit)
terminalMenu.Append(itemQuit)

rootMenu.AppendSubMenu(terminalMenu,"Terminate Operations","Exit Session")

top =wx.Frame(None, title="Base Window",size=((1400,875)), style=wx.CAPTION|wx.RESIZE_BORDER|wx.WS_EX_PROCESS_IDLE)
top.Bind(wx.EVT_RIGHT_DOWN, OnRightDown)
top.Centre()
top.Show()
app.SetTopWindow(top)
app.MainLoop()

问题是,菜单的事件处理程序均未触发。它们必须存在,如果我将其注释掉,则会出现预期的错误,但存根处理程序不会输出到控制台,并且不会触发“ Quit”事件对话框(为测试目的而添加)。

我到底想念什么?

欢呼

0 个答案:

没有答案
相关问题