Tkinter Menu(菜单)

  Tkinter Menu: 这个小工具的目标是,让我们来创建我们的应用程序,可以通过使用各种菜单。核心功能,提供的方式来创建三个菜单类型:弹出式,顶层,和下拉
 
这个小工具的目标是,让我们来创建我们的应用程序,可以通过使用各种菜单。核心功能,提供的方式来创建三个菜单类型:弹出式,顶层,和下拉.

它也有可能使用其他的扩展部件,以实现新类型的菜单,如OptionMenu部件,实现的一种特殊类型,生成一个项目的弹出列表内选择.

语法:

这里是一个简单的语法来创建这个widget:

 w = Menu ( master, option, ... )

参数:

  • master: 这代表了父窗口.

  • options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.

OptionDescription
activebackgroundThe background color that will appear on a choice when it is under the mouse.
activeborderwidthSpecifies the width of a border drawn around a choice when it is under the mouse. Default is 1 pixel.
activeforegroundThe foreground color that will appear on a choice when it is under the mouse.
bgThe background color for choices not under the mouse.
bdThe width of the border around all the choices. Default is 1.
cursorThe cursor that appears when the mouse is over the choices, but only when the menu has been torn off.
disabledforegroundThe color of the text for items whose state is DISABLED.
fontThe default font for textual choices.
fgThe foreground color used for choices not under the mouse.
postcommandYou can set this option to a procedure, and that procedure will be called every time someone brings up this menu.
reliefThe default 3-D effect for menus is relief=RAISED.
imageTo display an image on this menubutton.
selectcolorSpecifies the color displayed in checkbuttons and radiobuttons when they are selected.
tearoffNormally, 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.
titleNormally, 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.

方法:

这些方法是在菜单对象:

OptionDescription
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()

这将产生以下结果:

 

转载于:https://www.cnblogs.com/tkinter/p/5628854.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值