python tkinter 使用(十)
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
@Author: zh
@Time 2023/11/27 下午3:36 .
@Email:
@Describe:
"""
import tkinter
from tkinter import *
master = Tk()
master.title("菜单")
master.geometry("500x500")
variable = StringVar(master)
variable.set("Option 1") # 默认选项
options = ["Option 1", "Option 2", "Option 3","Option 4", "Option 5", "Option 6"]
w = OptionMenu(master, variable, *options)
w.pack()
mainloop()
- 定义一个变量variable,用于存储选中的选项
- 定义选项列表
- 创建OptionMenu
另外我们可以通过打印变量variable,获取menu当前选中的值.
def menuValues(event):
print(variable.get())
btn = tkinter.Button(master,text="菜单打印")
btn.bind('<1>',menuValues)
btn.pack()
删除菜单:
def deleteMenu():
w["menu"].delete(2,"end")
tkinter.Button(text="删除菜单", command=deleteMenu).pack()
标签:菜单,tkinter,Option,python,master,使用,variable
From: https://www.cnblogs.com/zhjing/p/17929274.html