[英]Tkinter Option menu and config
我正在尝试制作一个具有两个下拉菜单和一个文本标签的 GUI。 在第一个下拉菜单中选择的值应更新第二个下拉菜单中的选项列表。 (这部分代码工作正常!)。 然后,一旦用户从第二个下拉菜单中选择了一个值,文本标签就会更新。 我正在尝试使用 config 方法更新文本标签,但它似乎不起作用。 有任何想法吗?
import tkinter as tk
from tkinter import *
root = Tk()
root.title("My app")
root.minsize(width=330,height=280)
options1 = ["Option 1","Option 2"]
options2 = [""]
options21 =["Option 1.1","Option 1.2","Option 1.3","Option 1.4"]
options22 = ["Option 2.1","Option 2.2","Option 2.3","Option 2.4"]
labelvuot=Label(root,text= " ")
def weigthtxt(event):
if om2.get() == "":
pass
else:
mydamage = int(om2.get().split(".")[1])
if mydamage == 1:
labelweigth.config(text="1")
elif mydamage == 2:
labelweigth.config(text="2")
elif mydamage == 3:
labelweigth.config(text="3")
elif mydamage == 4:
labelweigth.config(text="4")
else:
labelweigth.config(text="No number")
def add_option(self):
om2.set("")
labelweigth.config(text="")
answer = om1.get()
global options2
options2.clear()
if answer == "Option 1":
options2 = options2+options21
menu = drop2["menu"]
menu.delete(0, "end")
for x in options2:
menu.add_command(label=x,
command=lambda value=x: om2.set(value))
elif answer == "Option 2":
options2 = options2 + options22
menu = drop2["menu"]
menu.delete(0, "end")
for x in options2:
menu.add_command(label=x,
command=lambda value=x: om2.set(value))
def save():
element = om1.get()
damage = om2.get()
inten = r1.get()
exten = r2.get()
pergiu = r3.get()
txt = note.get(1.0,END)
print(element,damage,inten,exten,pergiu,txt,int(om2.get().split(".")[1]))
label1=Label(root,text= "Select Element").grid(row=1,column=3)
labelvuot.grid(row=1,column=5)
om1 = tk.StringVar()
om1.set("")
drop = tk.OptionMenu(root, om1, *options1, command= add_option)
drop.config(width=20)
drop.grid(row=1,column=7,columnspan=3)
label2=Label(root,text= "Damage Type").grid(row=2,column=3)
om2 = tk.StringVar()
om2.set("")
drop2 = tk.OptionMenu(root,om2, *options2,command=weigthtxt)
drop2.config(width=20)
drop2.grid(row=2,column=7,columnspan=3)
label3=Label(root,text= "Weigth").grid(row=3,column=1,columnspan=3)
labelweigth = Label(root,text="")
labelweigth.grid(row=3,column=7,columnspan=3)
root.mainloop()
tkinter
模块有一个内部类_setit
由tk.OptionMenu
类在设置菜单操作时使用。
当您在add_option()
函数中填充菜单项时,您需要使用这个内部类:
...
# it is executed when item of second dropdown menu is selected
def weigthtxt(value):
if value:
mydamage = int(value.split(".")[1])
if mydamage == 1:
labelweigth.config(text="1")
elif mydamage == 2:
labelweigth.config(text="2")
elif mydamage == 3:
labelweigth.config(text="3")
elif mydamage == 4:
labelweigth.config(text="4")
else:
labelweigth.config(text="No number")
def add_option(answer):
om2.set("")
labelweigth.config(text="")
options2.clear()
if answer == "Option 1":
options2.extend(options21)
elif answer == "Option 2":
options2.extend(options22)
menu = drop2["menu"]
menu.delete(0, "end")
for x in options2:
menu.add_command(label=x, command=tk._setit(om2, x, weigthtxt))
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.