2017-01-01 51 views
0

我是Python新手,但我想了解使用wxpython的GUI。我正在使用模板创建框架,并添加了菜单。显示菜单,但不会触发任何操作,所以我需要将操作绑定到我创建的不同菜单项。问题是,我不知道如何。绑定菜单事件wxpython

我开始菜单保存,我把它叫做“menu_open”,并关联到我使用相关联的动作方法

filemenu.Append(wx.ID_OPEN, "Open")

self.Bind(wx.EVT_MENU, self.Open, menu_open)

,但我得到了错误:

AttributeError: 'MainWindow' object has no attribute 'Open'

如果我尝试'OnOpen'(因为有'OnExit'属性),我得到的错误:

frame = MainWindow(None, "Sample editor")

AttributeError: 'MainWindow'object has no attribute 'OnOpen'

所以问题是:

  1. self.Bind语法正确,并分配到一个菜单中选择操作的正确方法?
  2. 是否有wxPython中可用菜单的完整属性列表?

我在报告整个代码以供参考。谢谢。 G.

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
from __future__ import print_function 

import wx 


class MainWindow(wx.Frame): 
    def __init__(self, parent, title): 
     wx.Frame.__init__(self, parent, title=title, size=(200, 100)) 
     self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE) 
     # A Statusbar in the bottom of the window 
     self.CreateStatusBar() 

     # Setting up the menus 
     '''Define main items''' 
     filemenu = wx.Menu() 
     editmenu = wx.Menu() 
     infomenu = wx.Menu() 
     '''Items''' 
     # file menu 
     menu_open = filemenu.Append(wx.ID_OPEN, "Open") 
     filemenu.Append(wx.ID_NEW, "New") 
     filemenu.Append(wx.ID_SAVE, "Save") 
     filemenu.Append(wx.ID_SAVEAS, "Save as") 
     filemenu.Append(wx.ID_EXIT, "Exit") 
     filemenu.AppendSeparator() 
     filemenu.Append(wx.ID_PRINT, "&Print") 
     filemenu.Append(wx.ID_PRINT_SETUP, "Print setup") 
     filemenu.Append(wx.ID_PREVIEW, "Preview") 
     # edit menu 
     editmenu.Append(wx.ID_COPY, "Copy") 
     editmenu.Append(wx.ID_CUT, "Cut") 
     editmenu.Append(wx.ID_PASTE, "Paste") 
     editmenu.AppendSeparator() 
     editmenu.Append(wx.ID_UNDO, "Undo") 
     editmenu.Append(wx.ID_REDO, "Re-do it") 
     # info menu 
     infomenu.Append(wx.ID_ABOUT, "About") 
     '''Bind items for activation''' 
     # bind file menu 
     self.Bind(wx.EVT_MENU, self.OnOpen, menu_open) 

     # Creating the menubar. 
     menuBar = wx.MenuBar() 
     # Add menus 
     menuBar.Append(filemenu, "&File") 
     menuBar.Append(editmenu, "&Edit") 
     menuBar.Append(infomenu, "&Help") 
     # Adding the MenuBar to the Frame content. 
     self.SetMenuBar(menuBar) 
     self.Show(True) 

app = wx.App(False) 
frame = MainWindow(None, "Sample editor") 
app.MainLoop() 

回答

2

您只需使用

self.Bind(wx.EVT_MENU, self.OnOpen, menu_open) 

时没有创建事件处理方法,所以你需要一个将被调用添加到类主窗口

def OnOpen(self, event): 
    print('OnOpen') 
+0

我认为这些方法'onOpen'等是从wx.MENU派生的标准方法,这就是为什么我也在寻找一个完整的方法列表... – Gigiux

+0

有一些标准的方法为各种小部件,像'wx.Frame',但他们不不会自动映射到菜单事件 –

+0

是否有列表可获取标准菜单项所需的操作代码,如打开,保存,另存为,新建等? – Gigiux