我的WxPython菜单有问题!在文件头中,我有“文件”和两个选项,“保存”和“关闭”…当我单击“保存”时,我希望它打印“已保存”,当我时钟退出时,应用程序应打印“已关闭”并关闭…但如果我甚至单击“保存”,它将关闭!你能帮我吗。以下是我代码的一部分:
menubar = wx.MenuBar()
fileMenu = wx.Menu()
menubar.Append ( fileMenu, '&File' )
m1 = fileMenu.Append ( wx.ID_EXIT, 'Save' )
self.Bind ( wx.EVT_MENU, self.OnSave, m1 )
m2 = fileMenu.Append ( wx.ID_EXIT, 'Quit' )
self.Bind ( wx.EVT_MENU, self.OnQuit, m2 )
self.SetMenuBar ( menubar )
以及功能:
def OnSave ( self, event ):
text = self.text_ctrl.GetValue()
fil.write ( text )
print ( "Saved file")
def OnQuit ( self, event ):
print ( "Closed" )
self.Close()
fileMenu.Append的第一个参数很重要。它用于区分一个菜单项和另一个菜单项,但您对这两个菜单项使用了相同的值
在我的程序中,我更喜欢使用wx.NewId()从系统中获取免费id。如果您有这些“库存”菜单项,则ID_SAVE和ID_EXIT非常有意义,但如果您有自己的条目,则可以执行以下操作:
您对两个菜单项使用相同的
Id
,即wx.ID_EXIT
,正如@Petr Blahos所指出的,这会导致您的问题。然而,Petr的回答存在一个问题,尽管在技术上是正确的,
wx.NewId
现在是Deprecated
,尽管它仍然有效,但它已被wx.NewIdRef
取代wx.NewIdRef
还有一个count
函数,因此它可以用于grab
一组您需要使用的id。i、 e注意
wx.NewIdRef
、wx.ID_ANY
和-1
都可以在这个菜单上下文中使用。请参见以下简单示例:相关问题 更多 >
编程相关推荐