wx.menubar

迈开步子 — wx.MenuBar

在你的 wxPython 应用程序中设置一个菜单栏是相当简单的. 下面将讨论在菜单栏(menubar)中添加菜单以及在已有的菜单中添加子菜单(submenu). 每个菜单都包含了菜单条目(menuitems). 菜单条目可以是普通条目、复选条目(check item)或 radio 条目1.[more…]
首先要做的是创建一个菜单栏.

menubar = wx.MenuBar ()

接着要创建我们需要的菜单.

file = wx.Menu()
edit = wx.Menu()
help = wx.Menu()

再要将一些条目添加进菜单中. 这可以两种方式完成.

file.Append(101, '&Open', 'Open a new document')
file.Append(102, '&Save', 'Save the document')

我们可以用一条横线将菜单项目分隔为逻辑上的区段.

file.AppendSeparator()

如你想要在你的菜单项目中有一些图标,你可以手动创建 MenuItem 对象,这是添加菜单项目的第二种方法.

quit = wx.MenuItem(file, 105, '&Quit\tCtrl+Q', 'Quit the Application')
quit.SetBitmap(wx.Image('Stock_exit-16.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
file.AppendItem(quit)

wxPython 工具包只能将位图 (bitmap) 放入菜单. 因此我们需要将 PNG 文件转换为位图.
此时将菜单添加进菜单栏.

menubar.Append(file, '&File') 
menubar.Append(edit, '&Edit') 
menubar.Append(help, '&Help')

最后我们要在我们的应用程序类中设置上我们的菜单栏.

self.SetMenuBar(menubar)

让我们把这些代码放进一小段脚本中吧.

#!/usr/bin/python 
#coding=utf-8 

import wx 

class MyMenu(wx.Frame): 
    def __init__(self, parent, id, title): 
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(200, 150)) 

        menubar = wx.MenuBar() 
        file = wx.Menu() 
        edit = wx.Menu() 
        help = wx.Menu() 
        file.Append(101, '&Open', '打开一个新的文档') 
        file.Append(102, '&Save', '保存文档') 
        file.AppendSeparator() 
        quit = wx.MenuItem(file, 105, '&Quit\tCtrl+Q', '退出程序') 
        quit.SetBitmap(wx.Image('./icons/stock_exit-16.png', 
                                wx.BITMAP_TYPE_PNG).ConvertToBitmap()) 
        file.AppendItem(quit)

        menubar.Append(file, '&File') 
        menubar.Append(edit, '&Edit') 
        menubar.Append(help, '&Help')  
        self.SetMenuBar(menubar) 
        self.CreateStatusBar() 

class MyApp(wx.App): 
    def OnInit(self): 
        frame = MyMenu(None, -1, 'menu1.py') 
        frame.Show() 
        return True 

app = MyApp(0) 
app.MainLoop()

到目前为止,我们看到的是默认的、普通的菜单条目. 下面我们来看看怎样显式地定义复选条目、普通条目以及 radio 条目.

edit.Append(201, 'check item1', '', wx.ITEM_CHECK) 
edit.Append(202, 'check item2', kind=wx.ITEM_CHECK)

或者

Quit = wxMenuItem(file, 105, '&Quit\tCtrl+Q', 
                  'Quit the Application', wx.ITEM_NORMAL)

该参数叫做 kind.
下面是可用的 wx.ItemKind:

  • wx.ITEM_NORMAL —— 默认条目
  • wx.ITEM_CHECK —— 复选条目
  • wx.ITEM_RADIO —— radio 条目

如你想要创建一个子菜单,你可以先创建一个菜单.

submenu = wx.Menu()

再将一些菜单条目追加到这个子菜单.

submenu.Append(301, 'radio item1', 'radio 条目1', kind=wx.ITEM_RADIO) 
submenu.Append(302, 'radio item2', kind=wx.ITEM_RADIO) 
submenu.Append(303, 'radio item3', kind=wx.ITEM_RADIO)

最后把这个子菜单添加到一个菜单对象里去.

edit.AppendMenu(203, 'submenu', submenu)

现在我们来讨论一下怎样对用户行为进行响应. 这里我们只会略为触及,后面的章节将详细对其进行解释.
当使用者选择一个菜单条目时,一个事件就产生了. 我们务必要提供一个事件处理器(event handler), 由其对该事件进行相应处理. 到目前为止,我发现相比于其它工具包,wxPython 中对事件的处理是最为优雅和简单的. 当我们翻看 wxPython 的参考书, 我们会在事件处理章节找到 wx.EVT_MENU 处理器.
假设我们要在退出菜单上添加一个时间处理器.

wx.EVT_MENU(self, 105, self.OnQuit)

我们需要提供三个信息,一是我们绑定事件处理器所在的对象. 这里就是 self, 也就是应用程序的主对象; 二是需要响应的菜单条目的标示符(id); 以及完成具体工作的方法名.
而对用户动作进行响应的方法有两个参数. 第一个是该方法定义所在的对象. 第二个就是产生的事件. 这里用不到第二个参数. 我们只是简单的关闭我们的程序.

def OnQuit(self, event):
    self.Close()

下面的脚本对各种菜单条目、子菜单以及一个简单的事件处理器做了演示. 我(作者本人)讨厌由那全能窗口管理器的意志来决定我的程序窗口在某个角落弹出. 因此我加上了这行:

self.Centre()

这样窗口就会在屏幕中间弹出.

f-03

图:menu1.py

#!/usr/bin/python 
#coding=utf-8 

import wx 

class MyMenu(wx.Frame): 
    def __init__(self, parent, id, title): 
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, 
                          wx.Size(200, 150)) 

        menubar = wx.MenuBar() 
        file = wx.Menu() 
        edit = wx.Menu() 
        help = wx.Menu() 
        file.Append(101, '&Open', '打开一个新的文档') 
        file.Append(102, '&Save', '保存文档') 
        file.AppendSeparator() 
        quit = wx.MenuItem(file, 105, '&Quit\tCtrl+Q', '退出程序') 
        quit.SetBitmap(wx.Image('./icons/stock_exit-16.png', 
                                wx.BITMAP_TYPE_PNG).ConvertToBitmap()) 
        file.AppendItem(quit)        
        edit.Append(201, 'check item1', '', wx.ITEM_CHECK) 
        edit.Append(202, 'check item2', kind=wx.ITEM_CHECK) 
        submenu = wx.Menu() 
        submenu.Append(301, 'radio item1', 
                       'radio 条目1', kind=wx.ITEM_RADIO) 
        submenu.Append(302, 'radio item2', kind=wx.ITEM_RADIO) 
        submenu.Append(303, 'radio item3', kind=wx.ITEM_RADIO) 
        edit.AppendMenu(203, 'submenu', submenu) 
        menubar.Append(edit, '&Edit') 
        menubar.Append(help, '&Help') 
        self.SetMenuBar(menubar)        
        self.CreateStatusBar() 
        self.Center() 
        self.Bind(wx.EVT_MENU, self.OnQuit, id=105) 

    def OnQuit(self, event): 
        self.Close() 
        menubar.Append(file, '&File') 

class MyApp(wx.App): 
    def OnInit(self): 
        frame = MyMenu(None, -1, 'menu2.py') 
        frame.Show() 
        return True 

app = MyApp(0) 
app.MainLoop() 
f-04

图:menu2.py


脚 注:

1)Radio button 组是一组显示循环选择的按钮,每一次只能选择其中一个项目。在同一个组里,如果选择了一个,那么其它的自动变成未选择状态。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, "多个菜单栏不同菜单界面的切换", size=(800, 600)) self.panel = wx.Panel(self) self.createMenuBar1() self.createMenuBar2() self.createMenuBar3() self.switchMenuBar1() def createMenuBar1(self): self.menuBar1 = wx.MenuBar() menuItem1 = wx.MenuItem(self.menuBar1, -1, '菜单1') self.menuBar1.Append(menuItem1, '菜单1') self.Bind(wx.EVT_MENU, self.onSwitchMenuBar1, menuItem1) self.SetMenuBar(self.menuBar1) self.panel1 = wx.Panel(self, style=wx.BORDER_SUNKEN) self.panel1.SetPosition((0, 25)) self.panel1.SetSize((800, 575)) def createMenuBar2(self): self.menuBar2 = wx.MenuBar() menuItem2 = wx.MenuItem(self.menuBar2, -1, '菜单2') self.menuBar2.Append(menuItem2, '菜单2') self.Bind(wx.EVT_MENU, self.onSwitchMenuBar2, menuItem2) self.panel2 = wx.Panel(self, style=wx.BORDER_SUNKEN) self.panel2.SetPosition((0, 25)) self.panel2.SetSize((800, 575)) def createMenuBar3(self): self.menuBar3 = wx.MenuBar() menuItem3 = wx.MenuItem(self.menuBar3, -1, '菜单3') self.menuBar3.Append(menuItem3, '菜单3') self.Bind(wx.EVT_MENU, self.onSwitchMenuBar3, menuItem3) self.panel3 = wx.Panel(self, style=wx.BORDER_SUNKEN) self.panel3.SetPosition((0, 25)) self.panel3.SetSize((800, 575)) def onSwitchMenuBar1(self, event): self.SetMenuBar(self.menuBar1) self.panel2.Hide() self.panel3.Hide() self.panel1.Show() def onSwitchMenuBar2(self, event): self.SetMenuBar(self.menuBar2) self.panel1.Hide() self.panel3.Hide() self.panel2.Show() def onSwitchMenuBar3(self, event): self.SetMenuBar(self.menuBar3) self.panel1.Hide() self.panel2.Hide() self.panel3.Show() def switchMenuBar1(self): self.SetMenuBar(self.menuBar1) self.panel2.Hide() self.panel3.Hide() self.panel1.Show() if __name__ == '__main__': app = wx.App() frame = MyFrame() frame.Show(True) app.MainLoop()报错TypeError: MenuItem(): argument 1 has unexpected type 'MenuBar'咋解决
04-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值