【问题标题】:bind menu events wxpython绑定菜单事件 wxpython
【发布时间】: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'

所以问题是:

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

【问题讨论】:

    标签: drop-down-menu wxpython action


    【解决方案1】:

    你根本没有创建事件处理方法所以在使用时

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

    您需要一个将被调用的方法添加到 MainWindow 类中

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

    【讨论】:

    • 我认为这些 'onOpen' 等是从 wx.MENU 派生的标准方法,这就是为什么我也在寻找完整的方法列表...
    • 各种小部件都有一些标准方法,例如wx.Frame,但它们不会自动映射到菜单事件
    • 是否有一个列表可以获取标准菜单项(例如打开、保存、另存为、新建等)所需操作的代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 2011-06-25
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多