目录
一、MenuButton简介
二、MenuButton语法及其参数
Menu Buttons Options
三、案例1:MenuButton控件添加三个checkButton
四、案例2:MenuButton控件添加三个radioButton,并输出其绑定的值
五、案例3:较新版的ttk库的MenuButton控件
一、MenuButton简介
- MenuButton控件在被按下时,弹出下拉菜单。 下拉菜单里面可以放置radioButton或者checkButton。
二、MenuButton语法及其参数
Menu Button Syntax:
MenuBttn = Menubutton(master, option..... )
Menu Buttons Options
A complete list of options for the Tkinter Menu Button. Note however, that some will only work with the newer “ttk” version. A short segment on this is included at the end of the tutorial.
No. | Option | Description |
---|---|---|
1 | activebackground | Color of the background when widget is under focus. |
2 | activeforeground | Color of the foreground when widget in under focus. |
3 | bg | Background color for the area around the widget. |
4 | bd | Size of the border around the widget. Default value is 2 pixels. |
5 | cursor | When the mouse is hovering over this widget, it can be changed to a special cursor type like an arrow or dot. |
6 | direction | Sets the direction of the text relative to the Menu button. Options are, LEFT, RIGHT and ‘above’. |
7 | fg | The color for the text. |
8 | height | The height of the widget in terms of text lines. |
9 | highlightcolor | The text color when the widget is under focus. |
10 | image | Used to display an image |
11 | justify | Specifies how the text is aligned within the widget. One of “left”, “center”, or “right”. |
12 | menu | Used to assign a menu object to the menu button. Allows for a list of options to be displayed. |
13 | padx | Amount of padding in terms of pixels in the left-right directions. |
14 | pady | Amount of padding in terms of pixels for the area above and below the widget. |
15 | relief | It specifies the type of the border. Default is Flat, other options include RAISED and SUNKEN . |
16 | State | Default is NORMAL. DISABLED causes the Widget to gray out and go inactive. |
17 | text | The text displayed next on the Menu Button. |
18 | textvariable | Allows us to assign a variable to the Menu Button. Text displayed on the Menu Button can be changed by manipulating this variable. |
19 | width | Width of the widget in terms of number of characters. |
三、案例1:MenuButton控件添加三个checkButton
-
from tkinter import *
-
-
root = Tk()
-
("200x150")
-
frame = Frame(root)
-
()
-
-
# 定义一个Menubutton,其实它就是控件,只不过这个控件点击会弹出菜单。
-
# relief=RAISED定义Menubutton控件的外观样式
-
MenuBttn = Menubutton(frame, text="Favourite food", relief=RAISED)
-
()
-
-
Var1 = IntVar()
-
Var2 = IntVar()
-
Var3 = IntVar()
-
# Menubutton控件定义一个菜单Menu1,点击Menubutton就可以弹出Menu1菜单
-
Menu1 = Menu(MenuBttn, tearoff=0)
-
MenuBttn["menu"] = Menu1
-
-
# Menu1菜单里面定义三个checkButton控件
-
Menu1.add_checkbutton(label="Pizza", variable=Var1)
-
Menu1.add_checkbutton(label="Cheese Burger", variable=Var2)
-
Menu1.add_checkbutton(label="Salad", variable=Var3)
-
-
()
GUI效果:
四、案例2:MenuButton控件添加三个radioButton,并输出其绑定的值
-
from tkinter import *
-
-
def retrieve():
-
print(())
-
-
root = Tk()
-
("200x150")
-
frame = Frame(root)
-
()
-
-
MenuBttn = Menubutton(frame, text="Favourite food", relief=RAISED)
-
Menu1 = Menu(MenuBttn, tearoff=0)
-
MenuBttn["menu"] = Menu1
-
-
Var1 = IntVar()
-
Menu1.add_radiobutton(label="Pizza", variable=Var1, value=1, command=retrieve)
-
Menu1.add_radiobutton(label="Cheese Burger", variable=Var1, value=2, command=retrieve)
-
Menu1.add_radiobutton(label="Salad", variable=Var1, value=3, command=retrieve)
-
-
()
-
()
GUI效果:
五、案例3:较新版的ttk库的MenuButton控件
较新的 ttk 库带来了一些更新的控件,它们的性能通常比原始控件更好。 它们提供了更大的灵活性并有更多的选择。 比如说ttk版本的MenuButton可以绑定变量textvariable。
在下面的部分中,我们演示了如何执行此操作。
-
from tkinter import *
-
import tkinter
-
from ttk import Menubutton
-
-
def f():
-
var.set("Food")
-
-
root = Tk()
-
-
var = StringVar()
-
mb = Menubutton(root, textvariable=var)
-
()
-
= Menu(mb, tearoff=0)
-
mb["menu"] =
-
-
b = Button(root, text="Click", command=f)
-
()
-
-
mayoVar = IntVar()
-
ketchVar = IntVar()
-
-
.add_checkbutton(label="mayo", variable=mayoVar)
-
.add_checkbutton(label="ketchup",variable=ketchVar)
-
-
()
-
()
GUI效果: