【发布时间】:2017-01-01 23:01:51
【问题描述】:
我是 Python 新手,但我想了解使用 wxpython 使用 GUI。我正在使用模板来创建框架并添加了菜单。显示了菜单,但它们不会触发任何操作,因此我需要将操作绑定到我创建的不同菜单项。问题是,我不知道怎么做。
我从菜单保存开始,我称之为“menu_open”并与方法相关联
filemenu.Append(wx.ID_OPEN, "打开")
我使用以下方法关联了一个操作:
self.Bind(wx.EVT_MENU, self.Open, menu_open)
但我得到了错误:
AttributeError: 'MainWindow' 对象没有属性 'Open'
如果我尝试使用“OnOpen”(因为有一个“OnExit”属性),我会收到错误消息:
frame = MainWindow(None, "示例编辑器")
AttributeError: 'MainWindow' 对象没有属性 'OnOpen'
所以问题是:
- self.Bind 语法是否正确以及将操作分配给菜单的正确方法?
- 是否有 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()
【问题讨论】:
标签: drop-down-menu wxpython action