Tkinter (13) 选项菜单部件 OptionMenu
选项菜单部件的创建及其选项
该部件为Menubutton与Menu两部件在特定参数选项下所订立的部件
import tkinter as tk
parent = tk.Tk()
message = tk.OptionMenu(parent, variable, choice1, choice2, ...)
范例视窗及代码
import tkinter as tk
def func(select):
print(f'You select {select.get()}')
root = tk.Tk()
root.wm_title("Message Demo")
items = ('Train', 'Plane', 'Boat')
select = tk.StringVar()
select.set('Selection')
font = ('Courier New', 20, 'bold')
option_menu = tk.OptionMenu(root, select, *items)
option_menu.config(font=font, width=10)
option_menu.pack()
select.trace_add("write", lambda *_, select=select:func(select))
root.mainloop()
本作品采用《CC 协议》,转载必须注明作者和本文链接