wxpython入门(六)菜单项

wxpython入门(六)菜单项

参考书籍
http://wiki.woodpecker.org.cn/moin/WxPythonInAction

第十章 创建和使用wxPython菜单

创建菜单

如何创建一个菜单栏
wx.MenuBar构造函数:wx.MenuBar()

一旦菜单栏被创建了,使用SetMenuBar()方法将它附加给一个wx.Frame(或其子类)。通常这些都在框架的__init__或OnInit()方法中实施:
menubar = wx.MenuBar()
self.SetMenuBar

如何创建一个菜单并把它附加到菜单栏
wx.Menu的构造函数:
wx.Menu(title="", style=0)

examples:
import wx
class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "SimpleMenuExample")
        p = wx.Panel(self)
        menuBar = wx.MenuBar()# 创建一个菜单栏
        menu = wx.Menu()# 创建一个菜单
        menuBar.Append(menu, "Left")# 添加菜单到菜单栏
        menu2 = wx.Menu()
        menuBar.Append(menu2, "Middle")
        menu3 = wx.Menu()
        menuBar.Append(menu3, "Right")
        self.SetMenuBar(menuBar)
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

给下拉菜单添加项目,使用wx.Menu的Append()方法:
Append(id, string, helpStr="", kind=wx.ITEM_NORMAL)

examples:

import wx
class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Simple Menu Example")
        p = wx.Panel(self)
        self.CreateStatusBar()
        menu = wx.Menu()
        simple = menu.Append(-1, "Simple menu item", "This is some help text")
        menu.AppendSeparator() #分隔符
        exit = menu.Append(-1, "Exit")
        self.Bind(wx.EVT_MENU, self.OnSimple, simple)
        self.Bind(wx.EVT_MENU, self.OnExit, exit)
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "Simple Menu")
        self.SetMenuBar(menuBar)
    def OnSimple(self, event):
        wx.MessageBox("You selected the simple menu item")
    def OnExit(self, event):
        self.Close()
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

运行时添加菜单,examples:

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,"Add Menu Items")
        p = wx.Panel(self)
        self.txt = wx.TextCtrl(p, -1, "new item")
        btn = wx.Button(p, -1, "Add Menu Item")
        self.Bind(wx.EVT_BUTTON, self.OnAddItem, btn)# 绑定按钮的事件

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.txt, 0, wx.ALL, 20)
        sizer.Add(btn, 0, wx.TOP|wx.RIGHT, 20)
        p.SetSizer(sizer)
       
        self.menu = menu = wx.Menu()
        simple = menu.Append(-1, "Simple menu item")
        menu.AppendSeparator()
        exit = menu.Append(-1, "Exit")
        self.Bind(wx.EVT_MENU, self.OnSimple, simple)
        self.Bind(wx.EVT_MENU, self.OnExit, exit)
                  
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "Menu")
        self.SetMenuBar(menuBar)
      
    def OnSimple(self, event):
        wx.MessageBox("You selected the simple menu item")

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

    def OnAddItem(self, event):
        item = self.menu.Append(-1, self.txt.GetValue())# 添加菜单项
        self.Bind(wx.EVT_MENU, self.OnNewItemSelected, item)# 绑定一个菜单事件

    def OnNewItemSelected(self, event):
        wx.MessageBox("You selected a new item")

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

如何在一个菜单中找到一个特定的菜单项
examples:

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,"Find Item Example")
        p = wx.Panel(self)
        self.txt = wx.TextCtrl(p, -1, "new item")
        btn = wx.Button(p, -1, "Add Menu Item")
        self.Bind(wx.EVT_BUTTON, self.OnAddItem, btn)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.txt, 0, wx.ALL, 20)
        sizer.Add(btn, 0, wx.TOP|wx.RIGHT, 20)
        p.SetSizer(sizer)
       
        self.menu = menu = wx.Menu()
        simple = menu.Append(-1, "Simple menu item")
        menu.AppendSeparator()
        exit = menu.Append(-1, "Exit")
        self.Bind(wx.EVT_MENU, self.OnSimple, simple)
        self.Bind(wx.EVT_MENU, self.OnExit, exit)
                 
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "Menu")
        self.SetMenuBar(menuBar)

    def OnSimple(self, event):
        wx.MessageBox("You selected the simple menu item")

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

    def OnAddItem(self, event):
        item = self.menu.Append(-1, self.txt.GetValue())
        self.Bind(wx.EVT_MENU, self.OnNewItemSelected, item)

    def OnNewItemSelected(self, event):
        item = self.GetMenuBar().FindItemById(event.GetId()) #得到菜单项  
        text = item.GetText()
        wx.MessageBox("You selected the '%s' item" % text)
      
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

控制一个菜单的有效和无效
examples:

import wx

ID_SIMPLE = wx.NewId()

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Enable/Disable Menu Example")
        p = wx.Panel(self)
        self.btn = wx.Button(p, -1, "Disable Item", (20,20))
        self.Bind(wx.EVT_BUTTON, self.OnToggleItem, self.btn)
       
        menu = wx.Menu()
        menu.Append(ID_SIMPLE, "Simple menu item")
        self.Bind(wx.EVT_MENU, self.OnSimple, id=ID_SIMPLE)

        menu.AppendSeparator()
        menu.Append(wx.ID_EXIT, "Exit")
        self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
                 
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "Menu")
        self.SetMenuBar(menuBar)
       
    def OnSimple(self, event):
        wx.MessageBox("You selected the simple menu item")

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

    def OnToggleItem(self, event):
        menubar = self.GetMenuBar()
        enabled = menubar.IsEnabled(ID_SIMPLE)
        menubar.Enable(ID_SIMPLE, not enabled)
        self.btn.SetLabel((enabled and "Enable" or "Disable") + " Item")

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

菜单项与快捷键的关联(略)

开关菜单项
examples:

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Toggle Items Example")
        p = wx.Panel(self)
        menuBar = wx.MenuBar()
        menu = wx.Menu()
        exit = menu.Append(-1,"Exit")
        self.Bind(wx.EVT_MENU,self.OnExit,exit)
        menuBar.Append(menu, "Menu")

        menu = wx.Menu()
        menu.AppendCheckItem(-1, "CheckItem1")
        menu.AppendCheckItem(-1, "CheckItem2")
        menu.AppendCheckItem(-1, "CheckItem3")
        menu.AppendSeparator()
        menu.AppendRadioItem(-1, "RadioItem1")
        menu.AppendRadioItem(-1, "RadioItem2")
        menu.AppendRadioItem(-1, "RadioItem3")
        menuBar.Append(menu, "Toggle Items")
        self.SetMenuBar(menuBar)
       
    def OnExit(self, event):
        self.Close()

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

嵌套的子菜单

examples:

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Sub-menu Example")
        p = wx.Panel(self)
        menu = wx.Menu()

        submenu = wx.Menu()
        submenu.Append(-1,"Sub-item 1")
        submenu.Append(-1,"Sub-item 2")
        menu.AppendMenu(-1,"Sub-menu", submenu)#添加子菜单
       
        menu.AppendSeparator()
        exit = menu.Append(-1, "Exit")
        self.Bind(wx.EVT_MENU, self.OnExit, exit)
                 
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "Menu")
        self.SetMenuBar(menuBar)

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

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

右键弹出式菜单
examples:

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Popup Menu Example")
        self.panel = p = wx.Panel(self)
                 
        wx.StaticText(p, -1,
            "Right-click on the panel to show a popup menu",
            (25,25))

        self.popupmenu = wx.Menu()#创建一个菜单
        for text in "one two three four five".split():#填充菜单
            item = self.popupmenu.Append(-1, text)
            self.Bind(wx.EVT_MENU, self.OnPopupItemSelected, item)
        p.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup)#绑定一个显示菜单事件

    def OnShowPopup(self, event):#弹出显示
        pos = event.GetPosition()
        pos = self.panel.ScreenToClient(pos)
        self.panel.PopupMenu(self.popupmenu, pos)

    def OnPopupItemSelected(self, event):
        item = self.popupmenu.FindItemById(event.GetId())
        text = item.GetText()
        wx.MessageBox("You selected item '%s'" % text)

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

自定义的菜单
拷贝了一个小的png小图在菜单前面,examples:

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Fancier Menu Example")
        p = wx.Panel(self)
        menu = wx.Menu()

        bmp = wx.Bitmap("open.png", wx.BITMAP_TYPE_PNG)
        item = wx.MenuItem(menu, -1, "Open")
        item.SetBitmap(bmp)#增加一个自定义的位图
        menu.AppendItem(item)

        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        font.SetWeight(wx.BOLD)
        item = wx.MenuItem(menu, -1, "Has Bold Font")
        item.SetFont(font)#改变字体
        menu.AppendItem(item)

        item = wx.MenuItem(menu, -1, "Has Red Text")
        item.SetTextColour("red")#改变文本颜色
        menu.AppendItem(item)
           
        menu.AppendSeparator()
        exit = menu.Append(-1, "Exit")
        self.Bind(wx.EVT_MENU, self.OnExit, exit)
                 
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "Menu")
        self.SetMenuBar(menuBar)

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

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

你可能感兴趣的:(框架,wxPython)