Tkinter Menu(菜单)
它也有可能使用其他的扩展部件,以实现新类型的菜单,如OptionMenu部件,实现的一种特殊类型,生成一个项目的弹出列表内选择.
语法:
这里是一个简单的语法来创建这个widget:
w = Menu ( master, option, ... )
参数:
master: 这代表了父窗口.
options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.
| Option | Description |
|---|---|
| activebackground | The background color that will appear on a choice when it is under the mouse. |
| activeborderwidth | Specifies the width of a border drawn around a choice when it is under the mouse. Default is 1 pixel. |
| activeforeground | The foreground color that will appear on a choice when it is under the mouse. |
| bg | The background color for choices not under the mouse. |
| bd | The width of the border around all the choices. Default is 1. |
| cursor | The cursor that appears when the mouse is over the choices, but only when the menu has been torn off. |
| disabledforeground | The color of the text for items whose state is DISABLED. |
| font | The default font for textual choices. |
| fg | The foreground color used for choices not under the mouse. |
| postcommand | You can set this option to a procedure, and that procedure will be called every time someone brings up this menu. |
| relief | The default 3-D effect for menus is relief=RAISED. |
| image | To display an image on this menubutton. |
| selectcolor | Specifies the color displayed in checkbuttons and radiobuttons when they are selected. |
| tearoff | Normally, a menu can be torn off, the first position (position 0) in the list of choices is occupied by the tear-off element, and the additional choices are added starting at position 1. If you set tearoff=0, the menu will not have a tear-off feature, and choices will be added starting at position 0. |
| title | Normally, the title of a tear-off menu window will be the same as the text of the menubutton or cascade that lead to this menu. If you want to change the title of that window, set the title option to that string. |
方法:
这些方法是在菜单对象:
| Option | Description |
|---|---|
| add_command (options) | Adds a menu item to the menu. |
| add_radiobutton( options ) | Creates a radio button menu item. |
| add_checkbutton( options ) | Creates a check button menu item. |
| add_cascade(options) | Creates a new hierarchical menu by associating a given menu to a parent menu |
| add_separator() | Adds a separator line to the menu. |
| add( type, options ) | Adds a specific type of menu item to the menu. |
| delete( startindex [, endindex ]) | Deletes the menu items ranging from startindex to endindex. |
| entryconfig( index, options ) | Allows you to modify a menu item, which is identified by the index, and change its options. |
| index(item) | Returns the index number of the given menu item label. |
| insert_separator ( index ) | Insert a new separator at the position specified by index. |
| invoke ( index ) | Calls the command callback associated with the choice at position index. If a checkbutton, its state is toggled between set and cleared; if a radiobutton, that choice is set. |
| type ( index ) | Returns the type of the choice specified by index: either "cascade", "checkbutton", "command", "radiobutton", "separator", or "tearoff". |
例子:
请自行尝试下面的例子:
from Tkinter import *
def donothing():
filewin = Toplevel(root)
button = Button(filewin, text="Do nothing button")
button.pack() root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing) filemenu.add_separator() filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=donothing) editmenu.add_separator() editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
editmenu.add_command(label="Delete", command=donothing)
editmenu.add_command(label="Select All", command=donothing) menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu) root.config(menu=menubar)
root.mainloop()
这将产生以下结果:

Tkinter Menu(菜单)的更多相关文章
- tkinter中menu菜单控件(十二)
menu菜单控件 import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry("300x200 ...
- Tkinter 之Menu菜单标签
一.参数说明 语法 作用 MenuBar = tk.Menu(window) 创建一个菜单栏 fileBar = tk.Menu(MenuBar, tearoff=0) 创建一个菜单项,不分窗. Me ...
- Android开发中的menu菜单
复写onCreateOptionsMenu方法,当点击menu菜单时,调用该方法. @Override public boolean onCreateOptionsMenu(Menu menu) { ...
- SharePoint 2013 激活标题字段外的Menu菜单
前言 SharePoint 有个很特别的字段,就是标题(Title)字段,无论想要链接到项目,还是弹出操作项目的菜单,都是通过标题字段,一直以来需要的时候,都是通过将标题字段改名进行的. 其实,Sha ...
- Ecshop 后台增加一个左侧列表菜单menu菜单的方法
Ecshop 后台增加一个左侧列表菜单menu菜单需要修改三个文件:/admin/includes/inc_menu.php/admin/includes/inc_priv.php/languages ...
- [转]ExtJs4 笔记(13) Ext.menu.Menu 菜单、Ext.draw.Component 绘图、Ext.resizer.Resizer 大小变更
作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...
- 后台增加一个左侧列表菜单menu菜单的方法
Ecshop 后台增加一个左侧列表菜单menu菜单需要修改三个文件:/admin/includes/inc_menu.php/admin/includes/inc_priv.php/languages ...
- node-webkit教程<>Native UI API 之Menu(菜单)
node-webkit教程(6)Native UI API 之Menu(菜单)1 前言... 2 6.1 Menu 概述... 3 6.2 menu api6 6.2.1 new Menu([o ...
- Android4.0 -- UI控件之 Menu 菜单的的使用(三)
上一讲 [Android 开发]:UI控件之 Menu 菜单的的使用(二) 我们讲解了创建上下文菜单的第一种使用方式:Creating a floating context menu [创建悬浮的上下 ...
随机推荐
- 04-python第四天学习
(1)for循环里的else In [1]: nums = [11,22,33,44] In [2]: for temp in nums: ...: print(temp) ...: else: #e ...
- Postfix邮件黑名单和白名单
本文主要介绍如何用Postfix添加域名黑名单和白名单,用以处理垃圾邮件. 1.修改postfix主配置文件,增加限制语句 vim /etc/postfix/main.cf # 文末添加一行,限制往本 ...
- tcp流式套接字和udp数据报套接字编程区别
1. 流式套接字采用字节流方式进行传输,而数据报套接字 使用数据报形式传输数据2. tcp套接字会产生粘包,udp有消息边界,不会形成粘包.3. tcp编程可以保证消息的完整性,udp则不能保证4. ...
- 深入理解java虚拟机-第13章-线程安全与锁优化
第十三章 线程安全与锁优化 线程安全 java语言中的线程安全 1 不可变.Immutable 的对象一定是线程安全的 2 绝对线程安全 一个类要达到不管运行时环境如何,调用者都不需要额外的同步措施, ...
- HTML5移动Web开发指南-学习笔记(一)
一,浏览器引擎 浏览器的内核引擎,基本上是四分天下: Trident: IE 以Trident 作为内核引擎; Gecko: Firefox 是基于 Gecko 开发; WebKit: Safa ...
- BZOJ3144 Hnoi2013 切糕 【网络流】*
BZOJ3144 Hnoi2013 切糕 Description Input 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q列的 ...
- BZOJ1066 SCOI2007 蜥蜴 【网络流-最大流】
BZOJ1066 SCOI2007 蜥蜴 Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离 ...
- [BZOJ5338][TJOI2018]xor
bzoj luogu descirption 现在有一棵以 \(1\) 为根节点的由 \(n\) 个节点组成的树,树上每个节点上都有一个权值 \(v_i\) .现在有 \(Q\) 次操作,操作如下: ...
- MySQL 百万级分页优化(Mysql千万级快速分页)
以下分享一点我的经验 一般刚开始学SQL的时候,会这样写 : SELECT * FROM table ORDER BY id LIMIT 1000, 10; 但在数据达到百万级的时候,这样写会慢死 : ...
- 我常用的几个第三方 Python 库
转自:http://blog.csdn.net/gzlaiyonghao/article/details/2966811 wxPython 如果你之前是 windows 程序员,用 MFC 或者 WI ...