Python论坛  - 讨论区

标题:[python-chinese] wxPython动态菜单的问题

2006年06月04日 星期日 19:42

oyster lepto.python at gmail.com
Sun Jun 4 19:42:28 HKT 2006

前提(根据一个实例简化,所以看上去很奇怪,但是请不要改变这个前提)
根据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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年06月04日 星期日 22:10

杨城 yyangcheng at hotmail.com
Sun Jun 4 22:10:56 HKT 2006

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号