2006年06月04日 星期日 19:42
前提(根据一个实例简化,所以看上去很奇怪,但是请不要改变这个前提) 根据cfg=[['a', 'this is a'],['b', 'this is b']] 动态的产生菜单,其中a是菜单上显示的字,thi is a是传递给回调函数的字串 但是现在每次的输出都是this is b,我应该怎样修改? #!/usr/bin/env python # -*- coding: ISO-8859-1 -*- import wx class MyFrame(wx.Frame): def __init__(self, *args, **kwds): kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) # Menu Bar self.frame_1_menubar = wx.MenuBar() self.SetMenuBar(self.frame_1_menubar) self.mnFile = wx.Menu() cfg=[['a', 'this is a'],['b', 'this is b']] for i in cfg: exec 'self.IDmnFile%s =%d' % (i[0],wx.NewId() ) in locals() self.mnFile.Append(eval('self.IDmnFile%s' % i[0]), i[0], i[1]) pass self.frame_1_menubar.Append(self.mnFile, "File") # Menu Bar end self.__set_properties() self.__do_layout() for i in cfg: wx.EVT_MENU(self,eval('self.IDmnFile%s' % i[0]) , lambda e:self.event(i[1])) def __set_properties(self): self.SetTitle("frame_1") def __do_layout(self): sizer_1 = wx.BoxSizer(wx.VERTICAL) self.SetAutoLayout(True) self.SetSizer(sizer_1) sizer_1.Fit(self) sizer_1.SetSizeHints(self) self.Layout() def event(self, event): print 'event=',event class MyApp(wx.App): def OnInit(self): wx.InitAllImageHandlers() frame_1 = MyFrame(None, -1, "") self.SetTopWindow(frame_1) frame_1.Show() return 1 if __name__ == "__main__": app = MyApp(0) app.MainLoop() -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060604/719be341/attachment.html
2006年06月04日 星期日 22:10
oyster:您好! 下面是我修改的程序,主要修改点后我加入***做为提示。在消息中event应该不可以做为参数使用。 #!/usr/bin/env python # -*- coding: ISO-8859-1 -*- import wx class MyFrame(wx.Frame): def __init__(self, *args, **kwds): kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) # Menu Bar self.frame_1_menubar = wx.MenuBar() self.SetMenuBar(self.frame_1_menubar) self.mnFile = wx.Menu() cfg=[['a', 'this is a'],['b', 'this is b']] for i in cfg: exec 'self.IDmnFile%s =%d' % (i[0],wx.NewId() ) in locals() self.mnFile.Append(eval('self.IDmnFile%s' % i[0]), i[0], i[1]) pass self.frame_1_menubar.Append(self.mnFile , "File") # Menu Bar end self.__set_properties() self.__do_layout() for i in cfg: wx.EVT_MENU(self,eval('self.IDmnFile%s' % i[0]), //*** self.event) //*** def __set_properties(self): self.SetTitle("frame_1") def __do_layout(self): sizer_1 = wx.BoxSizer(wx.VERTICAL) self.SetAutoLayout (True) self.SetSizer(sizer_1) sizer_1.Fit(self) sizer_1.SetSizeHints(self) self.Layout() def event(self, event): print 'event=', self.mnFile.GetHelpString(event.GetId()) //*** class MyApp( wx.App): def OnInit(self): wx.InitAllImageHandlers() frame_1 = MyFrame(None, -1, "") self.SetTopWindow(frame_1) frame_1.Show() return 1 if __name__ == "__main__": app = MyApp(0) app.MainLoop() ----- Original Message ----- From: oyster To: python-chinese at lists.python.cn Sent: Sunday, June 04, 2006 7:42 PM Subject: [python-chinese] wxPython动态菜单的问题 前提(根据一个实例简化,所以看上去很奇怪,但是请不要改变这个前提) 根据cfg=[['a', 'this is a'],['b', 'this is b']] 动态的产生菜单,其中a是菜单上显示的字,thi is a是传递给回调函数的字串 但是现在每次的输出都是this is b,我应该怎样修改? #!/usr/bin/env python # -*- coding: ISO-8859-1 -*- import wx class MyFrame(wx.Frame): def __init__(self, *args, **kwds): kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) # Menu Bar self.frame_1_menubar = wx.MenuBar() self.SetMenuBar(self.frame_1_menubar) self.mnFile = wx.Menu() cfg=[['a', 'this is a'],['b', 'this is b']] for i in cfg: exec 'self.IDmnFile%s =%d' % (i[0],wx.NewId() ) in locals() self.mnFile.Append(eval('self.IDmnFile%s' % i[0]), i[0], i[1]) pass self.frame_1_menubar.Append(self.mnFile , "File") # Menu Bar end self.__set_properties() self.__do_layout() for i in cfg: wx.EVT_MENU(self,eval('self.IDmnFile%s' % i[0]) , lambda e:self.event(i[1])) def __set_properties(self): self.SetTitle("frame_1") def __do_layout(self): sizer_1 = wx.BoxSizer(wx.VERTICAL) self.SetAutoLayout (True) self.SetSizer(sizer_1) sizer_1.Fit(self) sizer_1.SetSizeHints(self) self.Layout() def event(self, event): print 'event=',event class MyApp( wx.App): def OnInit(self): wx.InitAllImageHandlers() frame_1 = MyFrame(None, -1, "") self.SetTopWindow(frame_1) frame_1.Show() return 1 if __name__ == "__main__": app = MyApp(0) app.MainLoop() _______________________________________________ python-chinese Post: send python-chinese at lists.python.cn Subscribe: send subscribe to python-chinese-request at lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese
Zeuux © 2025
京ICP备05028076号