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(菜单)的更多相关文章

  1. tkinter中menu菜单控件(十二)

    menu菜单控件 import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry("300x200 ...

  2. Tkinter 之Menu菜单标签

    一.参数说明 语法 作用 MenuBar = tk.Menu(window) 创建一个菜单栏 fileBar = tk.Menu(MenuBar, tearoff=0) 创建一个菜单项,不分窗. Me ...

  3. Android开发中的menu菜单

    复写onCreateOptionsMenu方法,当点击menu菜单时,调用该方法. @Override public boolean onCreateOptionsMenu(Menu menu) { ...

  4. SharePoint 2013 激活标题字段外的Menu菜单

    前言 SharePoint 有个很特别的字段,就是标题(Title)字段,无论想要链接到项目,还是弹出操作项目的菜单,都是通过标题字段,一直以来需要的时候,都是通过将标题字段改名进行的. 其实,Sha ...

  5. Ecshop 后台增加一个左侧列表菜单menu菜单的方法

    Ecshop 后台增加一个左侧列表菜单menu菜单需要修改三个文件:/admin/includes/inc_menu.php/admin/includes/inc_priv.php/languages ...

  6. [转]ExtJs4 笔记(13) Ext.menu.Menu 菜单、Ext.draw.Component 绘图、Ext.resizer.Resizer 大小变更

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  7. 后台增加一个左侧列表菜单menu菜单的方法

    Ecshop 后台增加一个左侧列表菜单menu菜单需要修改三个文件:/admin/includes/inc_menu.php/admin/includes/inc_priv.php/languages ...

  8. 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 ...

  9. Android4.0 -- UI控件之 Menu 菜单的的使用(三)

    上一讲 [Android 开发]:UI控件之 Menu 菜单的的使用(二) 我们讲解了创建上下文菜单的第一种使用方式:Creating a floating context menu [创建悬浮的上下 ...

随机推荐

  1. kd树的原理

      kd树就是一种对k维空间中的实例点进行存储以便对其进行快速检索的树形数据结构,可以运用在k近邻法中,实现快速k近邻搜索.构造kd树相当于不断地用垂直于坐标轴的超平面将k维空间切分.    假设数据 ...

  2. flutter笔记1:VScode安装dart code插件踩坑记录

    新手菜鸟一枚,想从产品转入技术坑,目标:移动端APP开发.最近听技术达人 飞狐 说flutter beta发布了,支持跨平台APP开发,各种强大易上手,于是乎零基础入坑~话说想提高英文水平的同学,请移 ...

  3. linux vim 命令使用

    基本上vim可以分为三种状态,分别是命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode) 模式切换方法 在命令模式输入“i”,进入插入模式 ...

  4. vector 中的clear()

    为什么clear之后,还是输出fdsafdsa.有什么办法可以真正清空之? 因为对于vector,clear并不真正释放内存(这是为优化效率所做的事),clear实际所做的是为vector中所保存的所 ...

  5. WinForm窗体继承自定义的模板窗体出错

    在开发Winform程序的时候,我们往往需要根据需要做一些自定义的控件模块,这样可以给系统模块重复利用,或者实现更好的效果等功能.而今天自定义一个窗体,然后子窗体继承的时候出现了一点问题. 问题: 在 ...

  6. LG1155 双栈排序

    题意 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈\(S_1\)和\(S_2\),Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈\(S_ ...

  7. 1.JMeter===添加响应断言

    断言即Lr中的检查点,我们在进行测试时,需要对每次请求测试的返回做检验 1.以百度做案例,添加线程组==添加HTTP请求==添加查看结果树 2.在HTTP请求下添加响应断言 注:模式匹配规则,比较常用 ...

  8. callback&&callback()

    如果存在回调函数就执行!这是利用了 JS &&符号的一个小技巧&& 符号在前面为假时就不会执行后面的语句了所以这个就相当于 if(callback){ callback ...

  9. stm32f0系列在SWD模式下载时复位失败

    用stm32f030K6T6做了个小玩意,仿真电路就直接把3.3V,SWDIO,SWCLK,GND引出来连接到j-link的这四个角上,SWDIO和SWCLK引脚既没有上拉也没有下拉.     MCU ...

  10. 可以随时查找的max栈和max队列——面试

    这是面试中比较常见的题目,max队列也是编程之美里的一道题 对于max的栈,有个比较简单的办法就是,每次入栈前判断栈顶元素与正在入栈的元素哪个大,哪个大就哪个入栈 对于队列,我们知道可以用两个栈来实现 ...