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()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要开始学习wxPython,首先你需要下载wxPython的安装文件,你可以在wxPython的官方网站上找到下载地址。接下来,你可以使用命令行工具通过以下命令下载和安装wxPython: ``` pip install -U -f https://wxpython.org/Phoenix/snapshot-builds/ wxPython ``` 这个命令会从指定的URL下载最新版本的wxPython,并将其安装到你的Python环境中。 一旦安装完成,你就可以开始学习基础知识了。你可以查阅wxPython的官方文档和教程,它们提供了很好的学习资源。你可以在wxPython的官方网站上找到官方文档,这个文档详细介绍了wxPython的各个方面。此外,还有一些比较好的进阶教程和书籍,比如《wxPython参考文档》、《The wxPython Cookbook》和《Python GUI编程实战》等[3]。这些教程将帮助你了解一些高级概念,如自定义控件、绘图、数据绑定、多线程和网络编程等。 希望这些资源能够帮助你入门wxPython,祝你学习顺利!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [wxPython入门](https://blog.csdn.net/weixin_42515225/article/details/113550696)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [【Python wxPython】零基础也能轻松掌握的学习路线与参考资料](https://blog.csdn.net/weixin_50409347/article/details/130982779)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值