python TKinter弹出式菜单的实例方法
作者:小妮浅浅 时间:2023-03-25 05:59:54
1、弹出菜单也叫上下文菜单,建立菜单并向菜单添加各种功能。
2、右键监听鼠标。如右键点击,则根据位置判断弹出。
3、调用Menupop方法。
4、add_separator添加分隔符。
实例
# 弹出式菜单案例
import tkinter
def makeLabel():
global baseFrame
tkinter.Label(baseFrame, text="PHP是最好的编程语言,我用Python").pack()
baseFrame = tkinter.Tk()
menubar = tkinter.Menu(baseFrame)
for x in ['麻辣香菇', '汽锅鸡', '东坡肘子']:
menubar.add_separator()
menubar.add_command(label=x)
menubar.add_command(label="重庆火锅", command=makeLabel)
# 事件处理函数一定要至少有一个参数,且第一个参数表示的是系统事件
def pop(event):
# 注意使用 event.x 和 event.x_root 的区别
# menubar.post(event.x, event.y)
menubar.post(event.x_root, event.y_root)
baseFrame.bind("<Button-3>", pop)
baseFrame.mainloop()
实例扩展:
from tkinter import *
def sys_callbak():
pass
def fun_callbak():
pass
def no_thing(event):
popmenu.post(event.x_root,event.y_root)
master = Tk()
master.title('新闻自动抓取')
menubar = Menu(master)
sysmenu = Menu(menubar,tearoff=False)
sysmenu.add_command(label='添加',command=sys_callbak)
sysmenu.add_command(label='修改',command=sys_callbak)
sysmenu.add_separator()
sysmenu.add_command(label='退出',command=sys_callbak)
menubar.add_cascade(label='系统',menu=sysmenu)
funmenu = Menu(menubar)
funmenu.add_command(label='添加',command=fun_callbak)
funmenu.add_command(label='修改',command=fun_callbak)
menubar.add_cascade(label='功能',menu=funmenu)
popmenu = Menu(master)
popmenu.add_command(label='未设置',command=no_thing)
popmenu.add_command(label='想的美',command=no_thing)
frame = Frame(master,width=312,height=512)
frame.bind('<Button-3>',no_thing)
frame.grid()
master.grid()
master.config(menu=menubar)
mainloop()
来源:https://www.py.cn/jishu/jichu/34269.html
标签:python,TKinter,弹出式菜单
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL分库分表的几种方式
2024-01-22 05:25:36
![](https://img.aspxhome.com/file/2023/1/124871_0s.png)
深入理解JSON数据源格式
2024-05-10 14:06:09
前端必备插件之纯原生JS的瀑布流插件Macy.js
2024-02-27 09:05:59
![](https://img.aspxhome.com/file/2023/8/56458_0s.jpg)
python 字典的概念叙述和使用方法
2021-11-08 00:42:06
![](https://img.aspxhome.com/file/2023/2/114482_0s.png)
python 如何读取列表中字典的value值
2021-01-27 15:48:31
![](https://img.aspxhome.com/file/2023/1/120251_0s.png)
python微信跳一跳系列之棋子定位颜色识别
2023-01-16 04:52:49
![](https://img.aspxhome.com/file/2023/4/66814_0s.png)
使用Python+Splinter自动刷新抢12306火车票
2023-09-17 18:38:52
![](https://img.aspxhome.com/file/2023/8/91958_0s.jpg)
python jieba分词并统计词频后输出结果到Excel和txt文档方法
2023-10-28 21:16:37
![](https://img.aspxhome.com/file/2023/6/67846_0s.png)
教你为MySQL数据库换挡加速
2010-03-03 16:58:00
matplotlib之属性组合包(cycler)的使用
2021-04-16 19:31:04
![](https://img.aspxhome.com/file/2023/8/97348_0s.png)
MySQL中基本的用户和权限管理方法小结
2024-01-24 20:03:49
PyTorch Dataset与DataLoader使用超详细讲解
2021-05-12 16:43:43
![](https://img.aspxhome.com/file/2023/6/124306_0s.png)
python入门for循环嵌套理解学习
2021-03-01 21:42:16
![](https://img.aspxhome.com/file/2023/8/71388_0s.png)
Python+腾讯云服务器实现每日自动健康打卡
2023-08-18 00:22:44
![](https://img.aspxhome.com/file/2023/8/70678_0s.png)
pytorch加载语音类自定义数据集的方法教程
2021-07-15 20:38:07
Win10下Python3.7.3安装教程图解
2023-09-02 18:16:54
![](https://img.aspxhome.com/file/2023/6/62316_0s.jpg)
python基于SMTP发送QQ邮件
2023-04-07 21:11:51
![](https://img.aspxhome.com/file/2023/9/107019_0s.jpg)
状态机的概念和在Python下使用状态机的教程
2023-03-24 14:06:11
PHP获取类中常量,属性,及方法列表的方法
2023-11-19 19:57:58
numpy中的converters和usecols用法详解
2021-01-23 18:29:29
![](https://img.aspxhome.com/file/2023/9/102779_0s.png)