【问题标题】:Using tkinter to create drop down menu?使用 tkinter 创建下拉菜单?
【发布时间】:2017-12-10 04:13:43
【问题描述】:

我是 Python 新手,我正在尝试创建一个 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()

【问题讨论】:

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


【解决方案1】:

下拉小部件名为tkinter.OptionMenu。您需要创建一个可以更新标签的函数,并将该函数作为回调提供给 OptionMenu。像这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多