wxpython绑定来自进口的fi

2024-03-29 07:05:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将事件绑定到导入的函数?你知道吗

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)

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

您好


Tags: 函数posimportselfidsizetitleinit
1条回答
网友
1楼 · 发布于 2024-03-29 07:05:22

好吧,这里有一个方法。这是包含菜单代码的文件:

# 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()

相关问题 更多 >