惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
腾讯CDC
T
Tailwind CSS Blog
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
The Cloudflare Blog
D
DataBreaches.Net
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
B
Blog
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
博客园 - 司徒正美
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research

碎言

不懂就别瞎掰掰:【程序员的常识】写的什么玩意? AI 辅助编程下的程序设计与代码编写 使用 Next.js 和 Tailwind CSS 构建可编辑和删除的 ToDo 待办事项应用 探索编程新境界:MarsCode 助你一臂之力 使用 Next.js 和 Tailwind CSS 搭建静态图片展示站点并部署到 Vercel AI 辅助编程:免费工具的优缺点及官网一览 GitHub push更新总是失败,写个python脚本解决 把博客从GitHub迁移到到了vercel ComfyUI 和 Flux.1 安装与使用教程 Flux.1 入门必知:硬件、环境、模型 Git项目的子文件夹中的内容无法同步到远程仓库的解决方法 blender流体Fluid使用中没有流体、流体穿模等一些问题的解决方法 博客聚合网站:积薪,竟然关闭了! blender日常使用中的一些技巧 虽然只有我自己在用,但还是更新了碎言博客的源代码, 与其在迷茫中困惑,不如在努力中前进 差点忘了我还有一个博客... 秧歌、博客和AI 终于熬到了新手上路 好久没有更新博客了。。。 老妈大腿骨骨折,最近一直在医院护理 Hello, September! Ubuntu开机自动启动Docker容器运行WordPress Docker 简单快速安装部署WordPress Docker下安装MySQL 加碘盐能防核辐射的话,还怕什么核战争? shields.io 一个简洁、一致、清晰的徽章 Ubuntu下使用root登录ssh的设置 GitHub Actions 构建、部署 Next.js项目 我又用回了"IE"--edge
wxPython编程学习笔记(09)wx.Python Menu 菜单
J.sky · 2019-01-04 · via 碎言

输入图片说明

菜单是程序经常用到的小部件,我们来看看如何创建。

    self.menubar = wx.MenuBar()#创建一个程序菜单
    self.fileMenu = wx.Menu()#创建一个一级菜单,这个菜单里可以继续加入菜单,就可以产行二级菜单
    self.new = wx.MenuItem(self.fileMenu,9,"new")#创建菜单项
    self.fileMenu.Append(self.new)#添加菜单项

以上这几个操作就可以创建一个菜单及菜单项了

wxPython 右键菜单

创建方法,可以先创建一个菜单类class MyPopupMenu(wx.Menu) 然后再类中添加菜单项,最后在窗口程序中添加右键绑定一个事件:self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown) 然后事件函数中生成这个菜单self.PopupMenu(MyPopupMenu(self), e.GetPosition()) 这样,窗口程序中就有一个右键菜单了,完整的代码再下边,可以跑跑试试

import wx

class MyPopupMenu(wx.Menu):

    def __init__(self, parent):
        super(MyPopupMenu, self).__init__()

        self.parent = parent

        mmi = wx.MenuItem(self, wx.NewId(), 'Minimize')
        self.Append(mmi)
        self.Bind(wx.EVT_MENU, self.OnMinimize, mmi)

        cmi = wx.MenuItem(self, wx.NewId(), 'Close')
        self.Append(cmi)
        self.Bind(wx.EVT_MENU, self.OnClose, cmi)


    def OnMinimize(self, e):
        self.parent.Iconize()

    def OnClose(self, e):
        self.parent.Close()

class HelloFrame(wx.Frame):
    def __init__(self, *args, **kw):
        #调用父类的创建方法
        super(HelloFrame, self).__init__(*args, **kw)

        self.menubar = wx.MenuBar()#创建一个程序菜单
        self.fileMenu = wx.Menu()#创建一个一级菜单,这个菜单里可以继续加入菜单,就可以产行二级菜单
        self.new = wx.MenuItem(self.fileMenu,9,"new")#创建菜单项
        self.fileMenu.Append(self.new)#添加菜单项

        self.editMenu = wx.Menu()
        self.copyItem = wx.MenuItem(self.editMenu, 100, text="copy", kind=wx.ITEM_NORMAL)
        self.editMenu.Append(self.copyItem)
        self.cutItem = wx.MenuItem(self.editMenu, 101, text="cut", kind=wx.ITEM_NORMAL)
        self.editMenu.Append(self.cutItem)
        self.pasteItem = wx.MenuItem(self.editMenu, 102, text="paste", kind=wx.ITEM_NORMAL)
        self.editMenu.Append(self.pasteItem)

        self.fileMenu.Append(22, "Edit", self.editMenu)
        self.fileMenu.AppendSeparator()
        self.fileMenu.AppendSeparator()#分隔线
        self.qmi = wx.MenuItem(self.fileMenu, 1, '&Quit\tCtrl+Q')
        self.fileMenu.Append(self.qmi)

        self.Bind(wx.EVT_MENU, self.OnQuit, id=1)

        self.menubar.Append(self.fileMenu, '&File')
        self.SetMenuBar(self.menubar)

        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)

    def OnRightDown(self, e):
        self.PopupMenu(MyPopupMenu(self), e.GetPosition())

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

def main():
    app = wx.App(False)
    frm = HelloFrame(None, title='wx.MenuBar 菜单',)
    frm.Show()#显示窗口
    app.MainLoop()#持续更新窗口


if __name__ == '__main__':
        main()

本文源码下载: