Tkinter Menubutton(菜单按钮)

本文详细介绍了Python GUI库Tkinter中的Menubutton组件。Menubutton是一个下拉菜单按钮,用户点击时显示关联菜单。文章讲解了Menubutton的语法、参数配置,如背景颜色、字体颜色、方向设置等,并提供了示例代码展示如何使用Menubutton。
摘要由CSDN通过智能技术生成
Python - Tkinter Menubutton: 一个菜单按钮是一个下拉菜单,在屏幕上停留时间的一部分。菜单的小工具,可以显示该菜单按钮的选择,当用户点击它与每个menubutton时.
 
一个菜单按钮是一个下拉菜单,在屏幕上停留时间的一部分。菜单的小工具,可以显示该菜单按钮的选择,当用户点击它与每个menubutton时.

语法:

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

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

参数:

  • master: 这代表了父窗口.

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

OptionDescription
activebackgroundThe background color when the mouse is over the menubutton.
activeforegroundThe foreground color when the mouse is over the menubutton.
anchorThis options controls where the text is positioned if the widget has more space than the text needs. The default is anchor=CENTER, which centers the text.
bgThe normal background color displayed behind the label and indicator.
bitmapTo display a bitmap on the menubutton, set this option to a bitmap name.
bdThe size of the border around the indicator. Default is 2 pixels.
cursorThe cursor that appears when the mouse is over this menubutton.
directionSet direction=LEFT to display the menu to the left of the button; use direction=RIGHT to display the menu to the right of the button; or use direction='above' to place the menu above the button.
disabledforeground前景色显示在此菜单按钮时,它被禁用.
fgThe foreground color when the mouse is not over the menubutton.
heightThe height of the menubutton in lines of text (not pixels!). The default is to fit the menubutton's size to its contents.
highlightcolorColor shown in the focus highlight when the widget has the focus.
imageTo display an image on this menubutton,
justifyThis option controls where the text is located when the text doesn't fill the menubutton: use justify=LEFT to left-justify the text (this is the default); use justify=CENTER to center it, or justify=RIGHT to right-justify.
menuTo associate the menubutton with a set of choices, set this option to the Menu object containing those choices. That menu object must have been created by passing the associated menubutton to the constructor as its first argument.
padx多少空间留下的左边和右边的菜单按钮的文本。默认是1.
padyHow much space to leave above and below the text of the menubutton. Default is 1.
reliefSelects three-dimensional border shading effects. The default is RAISED.
stateNormally, menubuttons respond to the mouse. Set state=DISABLED to gray out the menubutton and make it unresponsive.
textTo display text on the menubutton, set this option to the string containing the desired text. Newlines ("\n") within the string will cause line breaks.
textvariableYou can associate a control variable of class StringVar with this menubutton. Setting that control variable will change the displayed text.
underlineNormally, no underline appears under the text on the menubutton. To underline one of the characters, set this option to the index of that character.
widthThe width of the widget in characters. The default is 20.
wraplengthNormally, lines are not wrapped. You can set this option to a number of characters and all lines will be broken into pieces no longer than that number.

例子:

自行尝试下面的例子:

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tk()

mb=  Menubutton ( top, text="condiments", relief=RAISED )
mb.grid()
mb.menu  =  Menu ( mb, tearoff = 0 )
mb["menu"]  =  mb.menu
    
mayoVar  = IntVar()
ketchVar = IntVar()

mb.menu.add_checkbutton ( label="mayo",
                          variable=mayoVar )
mb.menu.add_checkbutton ( label="ketchup",
                          variable=ketchVar )

mb.pack()
top.mainloop()

这将产生以下结果:

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值