使用tkinter创建下拉菜单?

时间:2017-12-10 04:13:44

标签: python python-3.x user-interface tkinter

我是Python新手,我正在尝试创建一个GUI,当选择下拉菜单中的项目时,该GUI会显示特征列表。我希望文本显示在下拉菜单下。这是我到目前为止所拥有的,但它提供的只是一个空盒子:

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()

1 个答案:

答案 0 :(得分:1)

下拉窗口小部件名为char *args[2]; args[0] = "ls"; args[1] = "sort"; size_t sz = sizeof args/sizeof args[0]; char *argsNew[sz]; for(size_t k = 0; k < sz ; ++k) { argsNew[k] = strdup(args[k]); printf("argsNew[%d] is %s\n", k, argsNew[k]); } 。您需要创建一个可以更新Label的函数,并将该函数作为回调提供给OptionMenu。像这样:

tkinter.OptionMenu