登录

gui - wxpython中,怎样创建可以绑定单击事件的一级菜单?

如题,尝试在一级菜单中绑定单机事件,但无论如何尝试都以失败告终.

发现在菜单追加的二级菜单是可以正常绑定事件的,但是一级菜单wx.Menu即使是绑定wx.EVTMENUOPEN事件也会导致无法显示,求解决!

编译环境:Eclipse+Pydev+Python2.7

我的代码如下,虽然编译通过了,但是无法正常显示:

#-*- coding: utf-8 -*-
import wx

class App(wx.App):
    def OnInit(self):
        self.frame = rpFrame(parent=None, id=-1)
        self.frame.Show()
        self.SetTopWindow(self.frame)

        return True

class rFrame(wx.Frame):
    def __init__(self, parent=None, id=-1):
        wx.Frame.__init__(self, parent, id,'标题')
#状态栏
        statusBar = self.CreateStatusBar() 
#菜单栏
        menuBar = wx.MenuBar()
        self.SetMenuBar(menuBar)
#菜单
        menu1 = wx.Menu()
        menuBar.Append(menu1, "文件")
        menu2 = wx.Menu()
        menuBar.Append(menu2, "退出")
#绑定事件
        self.Bind(wx.EVT_MENU, self.OnCloseMe, menu1)

    def OnCloseMe(self, event):
        self.Close(True)

if __name__ == '__main__':
    app = App()
    app.MainLoop()
# Python
怪我咯怪我咯2166 天前569 次浏览

全部回复(1) 我要回复

  • PHPzhong

    PHPzhong2017-04-17 11:43:53

    修改后的程序还是不能运行,有错误。

    Traceback (most recent call last):
      File "test.py", line 32, in <module>
        app = App()
      File "/usr/lib64/python2.7/site-packages/wx-2.9.4-gtk2/wx/_core.py", line 8631, in __init__
        self._BootstrapApp()
      File "/usr/lib64/python2.7/site-packages/wx-2.9.4-gtk2/wx/_core.py", line 8196, in _BootstrapApp
        return _core_.PyApp__BootstrapApp(*args, **kwargs)
      File "test.py", line 6, in OnInit
        self.frame = rFrame(parent=None, id=-1)
      File "test.py", line 26, in __init__
        self.Bind(wx.EVT_MENU, self.OnCloseMe, menu1)
      File "/usr/lib64/python2.7/site-packages/wx-2.9.4-gtk2/wx/_core.py", line 4225, in Bind
        assert source is None or hasattr(source, 'GetId')
    AssertionError
    

    应该是你 Bind 的用法有问题。

    回复
    0
  • 取消回复发送