snazzyslug
snazzyslug

Reputation: 1

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

Upvotes: 0

Views: 11583

Answers (1)

Novel
Novel

Reputation: 13729

The drop down widget is called tkinter.OptionMenu. You would need to make a function that can update the Label and provide that function to the OptionMenu as a callback. 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()

Upvotes: 2

Related Questions