0

如何将事件绑定到导入的函数?

import menuFunctions
import wx
import wx.lib.agw.aui as aui

class MyFrame(wx.Frame):
  def __init__(self, parent, id=-1, title='title', pos=wx.DefaultPosition, size=(800, 600),     style=wx.DEFAULT_FRAME_STYLE):
    wx.Frame.__init__(self, parent, id, title, pos, size, style)
    self._mgr = aui.AuiManager(self)

    self.Bind(wx.EVT_CLOSE, menuFunctions.onClose)

这是给我带来麻烦的最后一行

问候

4

1 回答 1

0

好吧,这是一种方法。这是其中包含菜单代码的文件:

# impMenu.py

import wx

#----------------------------------------------------------------------
def menu(self):
    """"""
    menuBar = wx.MenuBar()
    fileMenu = wx.Menu()
    exitMenuItem = fileMenu.Append(wx.NewId(), "Exit",
                                   "Exit the application")
    menuBar.Append(fileMenu, "&File")

    menuItems = [(exitMenuItem, "onExit")]

    return (menuBar, menuItems)

这是主程序代码:

import impMenu
import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Menu Test")
        panel = wx.Panel(self)

        menubar, menuItems = impMenu.menu(self)

        for item in menuItems:
            self.Bind(wx.EVT_MENU, getattr(self, item[1]), item[0])

        self.SetMenuBar(menubar)

    #----------------------------------------------------------------------
    def onExit(self, event):
        """"""
        print "in onExit!"

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    frame.Show()
    app.MainLoop()
于 2013-02-06T23:00:33.333 回答