wxpython:wx.Menu 菜单示例

pip install wxpython==4.2
wxPython-4.2.0-cp37-cp37m-win_amd64.whl (18.0 MB)
Successfully installed wxpython-4.2.0

cd \Python37\Scripts
wxdemo.exe 下载 wxPython-demo-4.2.0.tar.gz
wxdocs.exe   下载 wxPython-docs-4.2.0.tar.gz

编写 test_wx_menu.py 如下

# -*- coding: utf-8 -*-
import os
import sys
import time
import wx
import images 
# images.py 在 C:\Users\username\AppData\Local\wxPython\wxPython-demo-4.2.0\demo\
#-------------------------------------------------------------------class MyFrame(wx.Frame):def __init__(self, parent, id):wx.Frame.__init__(self, parent, id, 'Playing with menus', size=(600,300))self.CenterOnScreen()self.CreateStatusBar()self.SetStatusText("This is the statusbar")tc = wx.TextCtrl(self, -1, """
A bunch of bogus menus have been created for this frame.  You
can play around with them to see how they behave and then
check the source for this sample to see how to implement them.
""", style=wx.TE_READONLY|wx.TE_MULTILINE)# Prepare the menu barmenuBar = wx.MenuBar()# 1st menu from leftmenu1 = wx.Menu()menu1.Append(101, "&Mercury", "This the text in the Statusbar")menu1.Append(102, "&Venus", "")if False:# This is how you would create the menu item if you want to# change some of the visible attributes.item = wx.MenuItem(id=103, text="&Earth", helpString="You may select Earth too")item.SetFont(wx.Font(wx.FontInfo(10).Bold()))menu1.Append(item)else:# But we'll just do it the normal way for this samplemenu1.Append(103, "&Earth", "You may select Earth too")menu1.AppendSeparator()menu1.Append(104, "&Close", "Close this frame")# Add menu to the menu barmenuBar.Append(menu1, "&Planets")# 2nd menu from leftmenu2 = wx.Menu()menu2.Append(201, "Hydrogen")menu2.Append(202, "Helium")# a submenu in the 2nd menusubmenu = wx.Menu()submenu.Append(2031,"Lanthanium")submenu.Append(2032,"Cerium")submenu.Append(2033,"Praseodymium")menu2.Append(203, "Lanthanides", submenu)# Append 2nd menumenuBar.Append(menu2, "&Elements")menu3 = wx.Menu()# Radio itemsmenu3.Append(301, "IDLE", "a Python shell using tcl/tk as GUI", wx.ITEM_RADIO)menu3.Append(302, "PyCrust", "a Python shell using wxPython as GUI", wx.ITEM_RADIO)menu3.Append(303, "psi", "a simple Python shell using wxPython as GUI", wx.ITEM_RADIO)menu3.AppendSeparator()menu3.Append(304, "project1", "", wx.ITEM_NORMAL)menu3.Append(305, "project2", "", wx.ITEM_NORMAL)menuBar.Append(menu3, "&Shells")menu4 = wx.Menu()# Check menu itemsmenu4.Append(401, "letters", "abcde...", wx.ITEM_CHECK)menu4.Append(402, "digits", "123...", wx.ITEM_CHECK)menu4.Append(403, "letters and digits", "abcd... + 123...", wx.ITEM_CHECK)menuBar.Append(menu4, "Chec&k")menu5 = wx.Menu()# Show how to put an icon in the menu itemitem = wx.MenuItem(menu5, 500, "&Smile!\tCtrl+S", "This one has an icon")item.SetBitmap(images.Smiles.GetBitmap())menu5.Append(item)menuitemwithbmp = wx.MenuItem(menu5, wx.ID_ANY, "Submenu with Bitmap")# Show how to change the background colour of the menu itemmenuitemwithbmp.SetBackgroundColour(wx.YELLOW)# Show how to change the menu item's text colourmenuitemwithbmp.SetTextColour(wx.BLUE)# Show how to change the menu item's fontmenuitemwithbmp.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False, ''))submenu = wx.Menu(style=wx.MENU_TEAROFF)submenu.Append(wx.MenuItem(menu5, wx.ID_ANY, "Woot!"))menuitemwithbmp.SetBitmap(images.book.GetBitmap())menuitemwithbmp.SetSubMenu(submenu)menu5.Append(menuitemwithbmp)# Shortcutsmenu5.Append(501, "Interesting thing\tCtrl+A", "Note the shortcut!")menu5.AppendSeparator()menu5.Append(502, "Hello\tShift+H")menu5.AppendSeparator()menu5.Append(503, "remove the submenu")menu6 = wx.Menu()menu6.Append(601, "Submenu Item")menu5.Append(504, "submenu", menu6)menu5.Append(505, "remove this menu")menu5.Append(506, "this is updated")menu5.Append(507, "insert after this...")menu5.Append(508, "...and before this")menuBar.Append(menu5, "&Fun")self.SetMenuBar(menuBar)# Menu eventsself.Bind(wx.EVT_MENU_HIGHLIGHT_ALL, self.OnMenuHighlight)self.Bind(wx.EVT_MENU, self.Menu101, id=101)self.Bind(wx.EVT_MENU, self.Menu102, id=102)self.Bind(wx.EVT_MENU, self.Menu103, id=103)self.Bind(wx.EVT_MENU, self.CloseWindow, id=104)self.Bind(wx.EVT_MENU, self.Menu201, id=201)self.Bind(wx.EVT_MENU, self.Menu202, id=202)self.Bind(wx.EVT_MENU, self.Menu2031, id=2031)self.Bind(wx.EVT_MENU, self.Menu2032, id=2032)self.Bind(wx.EVT_MENU, self.Menu2033, id=2033)self.Bind(wx.EVT_MENU, self.Menu301To303, id=301)self.Bind(wx.EVT_MENU, self.Menu301To303, id=302)self.Bind(wx.EVT_MENU, self.Menu301To303, id=303)self.Bind(wx.EVT_MENU, self.Menu304, id=304)self.Bind(wx.EVT_MENU, self.Menu305, id=305)# Range of menu itemsself.Bind(wx.EVT_MENU_RANGE, self.Menu401To403, id=401, id2=403)self.Bind(wx.EVT_MENU, self.Menu500, id=500)self.Bind(wx.EVT_MENU, self.Menu501, id=501)self.Bind(wx.EVT_MENU, self.Menu502, id=502)self.Bind(wx.EVT_MENU, self.TestRemove, id=503)self.Bind(wx.EVT_MENU, self.TestRemove2, id=505)self.Bind(wx.EVT_MENU, self.TestInsert, id=507)self.Bind(wx.EVT_MENU, self.TestInsert, id=508)wx.GetApp().Bind(wx.EVT_UPDATE_UI, self.TestUpdateUI, id=506)# Methodsdef OnMenuHighlight(self, event):# Show how to get menu item info from this event handlerid = event.GetMenuId()item = self.GetMenuBar().FindItemById(id)if item:text = item.GetItemLabelText()help = item.GetHelp()# but in this case just call Skip so the default is doneevent.Skip()def Menu101(self, event):print('Welcome to Mercury\n')def Menu102(self, event):print('Welcome to Venus\n')def Menu103(self, event):print('Welcome to the Earth\n')def CloseWindow(self, event):self.Close()def Menu201(self, event):print('Chemical element number 1\n')def Menu202(self, event):print('Chemical element number 2\n')def Menu2031(self, event):print('Element number 57\n')def Menu2032(self, event):print('Element number 58\n')def Menu2033(self, event):print('Element number 59\n')def Menu301To303(self, event):id = event.GetId()print('Event id: %d\n' % id)def Menu304(self, event):print('Not yet available\n')def Menu305(self, event):print('Still vapour\n')def Menu401To403(self, event):print('From a EVT_MENU_RANGE event\n')def Menu500(self, event):print('Have a happy day!\n')def Menu501(self, event):print('Look in the code how the shortcut has been realized\n')def Menu502(self, event):print('Hello from Jean-Michel\n')def TestRemove(self, evt):mb = self.GetMenuBar()submenuItem = mb.FindItemById(601)if not submenuItem:returnsubmenu = submenuItem.GetMenu()menu = submenu.GetParent()# This works#menu.Remove(504)# this also worksmenu.RemoveItem(mb.FindItemById(504))# This doesn't work, as expected since submenuItem is not on menu#menu.RemoveItem(submenuItem)def TestRemove2(self, evt):mb = self.GetMenuBar()mb.Remove(4)def TestUpdateUI(self, evt):text = time.ctime()evt.SetText(text)def TestInsert(self, evt):theID = 508# get the menumb = self.GetMenuBar()menuItem = mb.FindItemById(theID)menu = menuItem.GetMenu()# figure out the position to insert atpos = 0for i in menu.GetMenuItems():if i.GetId() == theID:breakpos += 1# now insert the new itemID = wx.NewIdRef()item = wx.MenuItem(menu, ID, "NewItem " + str(ID))menu.InsertItem(pos, item)#---------------------------------------------------------------------------class TestPanel(wx.Panel):def __init__(self, parent, id):wx.Panel.__init__(self, parent, -1)b = wx.Button(self, -1, "Show the Menu sample", (50,50))self.Bind(wx.EVT_BUTTON, self.OnButton, b)def OnButton(self, evt):win = MyFrame(self, -1)win.Show(True)#-------------------------------------------------------------------
overview = """\
A demo of using wx.MenuBar and wx.Menu in various ways.A menu is a popup (or pull down) list of items, one of which may be selected
before the menu goes away (clicking elsewhere dismisses the menu). Menus may be
used to construct either menu bars or popup menus.A menu item has an integer ID associated with it which can be used to identify
the selection, or to change the menu item in some way. A menu item with a special
identifier -1 is a separator item and doesn't have an associated command but just
makes a separator line appear in the menu.Menu items may be either normal items, check items or radio items. Normal items
don't have any special properties while the check items have a boolean flag associated
to them and they show a checkmark in the menu when the flag is set. wxWindows
automatically toggles the flag value when the item is clicked and its value may
be retrieved using either IsChecked method of wx.Menu or wx.MenuBar itself or by
using wxEvent.IsChecked when you get the menu notification for the item in question.The radio items are similar to the check items except that all the other items
in the same radio group are unchecked when a radio item is checked. The radio group
is formed by a contiguous range of radio items, i.e. it starts at the first item of
this kind and ends with the first item of a different kind (or the end of the menu).
Notice that because the radio groups are defined in terms of the item positions
inserting or removing the items in the menu containing the radio items risks to not
work correctly. Finally note that the radio items are only supported under Windows
and GTK+ currently."""if __name__ == '__main__':app = wx.App(False) # False: 意味着不重定向标准输出和错误输出到窗口上frame = MyFrame(parent=None, id=wx.ID_ANY)frame.Show(True)app.MainLoop()

运行 python test_wx_menu.py

其中大段英文说明推荐用【百度翻译】软件翻译。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/54715.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

创建型模式-建造者模式

使用多个简单的对象一步一步构建成一个复杂的对象 主要解决:主要解决在软件系统中,有时候面临着"一个复杂对象"的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部…

ARM DIY(三)板载串口和 LCD 调试

前言 今天焊接两大关键输入输出设备:串口和屏幕。 串口 串口部分使用 CP2102N 芯片(USB 转 TTL),这样用一根数据线连接板子和 PC 就可以直接调试了。 焊接 CP2102 和 Type C 上电调试,串口可以正常输入输出。 看来…

使用Python写入数据到Excel:实战指南

在数据科学领域,Excel是一种广泛使用的电子表格工具,可以方便地进行数据管理和分析。然而,当数据规模较大或需要自动化处理时,手动操作Excel可能会变得繁琐。此时,使用Python编写程序将数据写入Excel文件是一个高效且便…

【办公类-16-01-02】2023年度上学期“机动班下午代班的排班表——跳过周三、节日和周末”(python 排班表系列)

背景需求: 2023年第一学期(2023年9-2024年1月),我又被安排为“机动班”,根据新学期的校历,手动推算本学期的机动班的带班表 排版原则 1、班级数量:共有6个班级,循环滚动 2、每周次…

安装启动yolo5教程

目录 一、下载yolo5项目 二、安装miniconda(建议不要安装在C盘) 三、安装CUDA 四、安装pytorch 五、修改配置参数 六、修改电脑参数 七、启动项目 博主硬件: Windows 10 家庭中文版 一、下载yolo5项目 GitHub - ultralytics/yolov5:…

使用EF Core更新与修改生产数据库

使用EF Core的Code First,在设计阶段,直接使用Database.EnsureCreated()和EnsureDeleted()可以快速删除、更新最新的数据结构。由于没有什么数据,删除的风险非常低。但是对于已经投入生产的数据库,这个方法就绝对不可行了。 考虑…

【C进阶】指针(一)

大家好,我是深鱼~ 【前言】: 指针的主题,在初阶指针章节已经接触过了,我们知道了指针的概念: 1.指针就是个变量,用来存放地址,地址的唯一标识一块内存空间(指针变量)&a…

集丰照明|汽车美容店设计,装修色彩灯光搭配方法

正确处理好店面的空间设计。 店铺各个功能区设计要合理,衔接合理,这样既能提高员工的工作效率也能提高顾客的满意度。合理安排店铺的空间分配, 要给顾客一种舒适度,既不能让顾客感觉到过于拥挤,又不能浪费店铺的有限空…

PostgreSQL系统概述

目录 写在前面 1.简介 1.1何为关系型数据库 1.2何为对象型数据库 2.特性 3.代码结构 3.1数据库集簇 3.2Parser查询分析流程 3.3内部查询树组成部分 3.3.1目标列表 3.4Optimizer查询优化流程 3.4.1查询计划 3.5非计划查询的SQL命令 写在前面 如有错误请指正&#xf…

.NET Core 实现日志打印输出在控制台应用程序中

在本文中,我们将探讨如何在 .NET Core 应用程序中将日志消息输出到控制台,从而更好地了解应用程序的运行状况。 .NET Core 实现日志打印输出在控制台应用程序中 在 .NET Core 中,日志输出打印是使用 Microsoft.Extensions.Logging 命名空间…

【LeetCode-面试经典150题-day14】

目录 19.删除链表的倒数第N个结点 82.删除排序链表中的重复元素Ⅱ 61. 旋转链表 86.分隔链表 146.LRU缓存 19.删除链表的倒数第N个结点 题意: 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。 【输入样例】head [1,2,3,4,5…

图形的透视矫正

概述 透视矫正是一种图像处理技术,用于将拍摄或者扫描得到的图像进行透视变换,以矫正不正确的形状和视角,从而得到正确的矩形形状。这一过程需要使用透视变换算法和线性插值技术。 在透视矫正的应用场景中,机器学习、深度学习等…

element-ui里el-table表格操作列多横线怎么解决

错误展示 错误原因 在vue中封装了element-ui表格,然后使用插槽,fixed定位等,导致样式出现了错乱 解决方案 1、线没有对齐 /* Element-UI 的table 组件出现表格线条不对齐的问题 */ body .el-table th.gutter {display: table-cell !impor…

基于MATLAB开发AUTOSAR软件应用层Code mapping专题-part 5 Signal/States标签页介绍

这一篇我们说下signals和State这两个怎么搞做映射,那首先我们要知道什么是Signal和state,我们看下模型, 在原来的模型里我增加了标红的圆圈处delay模块,这个delay模块就是一个state模块,表示离散的一个状态,这个是个模型的基本概念,后续我有个专栏交接simulink建模,那…

专题:平面、空间直线参数方程下的切线斜率问题

本文研究平面、空间直线在参数方程形式下,切线斜率(即导数)如何表示的问题。 如上图所示。 设 y f ( x ) , x φ ( t ) , y ψ ( t ) 当 t t 0 时, x x 0 , y y 0 ,即点 A 坐…

为Claude的分析内容做准备:提取PDF页面内容的简易应用程序

由于Claude虽然可以分析整个文件,但是对文件的大小以及字数是有限制的,为了将pdf文件分批传入Claude人工智能分析和总结文章内容,才有了这篇博客: 在本篇博客中,我们将介绍一个基于 wxPython 和 PyMuPDF 库编写的简易的…

【电路设计】单节锂电池使用

前言 最近在研究如何利用单节锂电池给3.3V单片机供电。 找到两个比较好的教程 单节锂电池如何转3.3V?升压还是降压? 锂电池接了保护板,就可以用五伏电压直接充电了吗? 其中上面提到的LDO,这里有一个型号&#xff1…

【校招VIP】测试专业课之TCP/IP模型

考点介绍: 大厂测试校招面试里经常会出现TCP/IP模型的考察,TCP/IP协议是网络基础知识,但是在校招面试中很多同学在基础回答中不到位,或者倒在引申问题里,就丢分了。 『测试专业课之TCP/IP模型』相关题目及解析内容可点…

git基本使用

1、创建仓库,提交代码 Git 全局设置: git config --global user.name "许歌" //全局绑定用户名 git config --global user.email "12075507xu-ge111user.noreply.gitee.com" //全局绑定邮箱创建 git 仓库: mkdir t…

TypeScript数组和对象的操作

TypeScript数组和对象的操作 一、数组的声明二、数组初始化三、数组元素赋值、添加、更改四、删除五、合并、断开数组六、查找数组元素位置七、连接数组元素八、排序、反序数组九、遍历请看这里 一、数组的声明 let arr1: Array<number>; let arr2: number[];二、数组初…