[英]Using tkinter to create drop down menu?
I'm new to Python, and I am trying to create a GUI that displays a list of characteristics when an item in a drop down menu is selected. 我是Python的新手,我试图创建一个GUI,当在下拉菜单中选择一项时,该GUI显示特征列表。 I want the text to be displayed under the drop down menu. 我希望文本显示在下拉菜单下。 Here is what I have so far, but all it provides is an empty box: 到目前为止,这是我所拥有的,但是它提供的只是一个空盒子:
import tkinter
import tkinter as tk
#creates box
window =tkinter.Tk()
frame= tkinter.Frame(window)
frame.pack()
window.geometry("%dx%d+%d+%d" % (330, 80, 200, 150))
window.title("Breeds and Characteristics")
#data
data=('Abyssinian','American-Bobtail','American-Curl')
Output1 ="Aloof,Intelligent,Diseased"
Output2= "Affectionate,Intelligent,Diseased"
Output3= "Affectionate,Dull,Healthy"
display = Label(window, text="")
#create a dropdown list
p = tkinter.Combobox(window, textvariable=var, values=data)
p.pack()
def chars():
for values in p:
if item == 'Abyssinian':
print (Output1)
elif item == 'American-Bobtail':
print (Output2)
elif item == 'American-Curl':
print (Output3)
#starts dropdown box at first cat
var = tkinter.StringVar()
var.set('Abyssinian')
#updates text
def boxtext():
display.configure(text=(chars))
display.pack()
#button to view characteristics
button = Button(window, text='View Characteristics', command=select)
button.pack(side='left', padx=20, pady=10)
window.mainloop()
The drop down widget is called tkinter.OptionMenu
. 下拉小部件称为tkinter.OptionMenu
。 You would need to make a function that can update the Label and provide that function to the OptionMenu as a callback. 您需要制作一个可以更新Label并将其作为回调提供给OptionMenu的函数。 Like this: 像这样:
import tkinter
#creates box
window =tkinter.Tk()
window.geometry("%dx%d+%d+%d" % (330, 80, 200, 150))
window.title("Breeds and Characteristics")
#data
data={
'Abyssinian':"Aloof,Intelligent,Diseased",
'American-Bobtail':"Affectionate,Intelligent,Diseased",
'American-Curl':"Affectionate,Dull,Healthy",
}
#updates text
def boxtext(new_value):
display.config(text = data[new_value])
#create a dropdown list
var = tkinter.StringVar()
var.set('Abyssinian')
p = tkinter.OptionMenu(window, var, *data, command=boxtext)
p.pack()
display = tkinter.Label(window)
display.pack()
window.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.