Reputation: 11
I'm playing a little bit with Python and tkinter in order to learn a little bit of programming. I wanted to try and create a Frame with a label, an image and a button. By clicking the button label-text and image should change, where a specific text and image are matched.
Changing the text was no problem, but changing the image doesnt work, and I dont really get why. I add the code to show how I've tried it. I get the error:
"_tkinter.TclError: image "dummy_logo2" doesn't exist"
What is the problem here? What am I overlooking?
root = tk.Tk()
dummy_logo2 = tk.PhotoImage(master=root, file='bell2.gif')
dummy_logo2 = dummy_logo2.subsample(5)
dummy_logo = tk.PhotoImage(master=root, file='bell.gif')
dummy_logo = dummy_logo.subsample(5)
dict_01 = {'eins':'dummy_logo',
'zwei':'dummy_logo',
'drei':'dummy_logo2',
'vier':'dummy_logo2'}
def choose():
exerc = random.choice(list(dict_01.keys()))
label_01.config(text=exerc)
label_02.config(image=dict_01[exerc])
label_01 = tk.Label(root, text='Dummy Text')
label_01.grid(row=1, column=1)
label_02 = tk.Label(root, image=dummy_logo)
label_02.grid(row=1, column=2)
button_01 = tk.Button(root, text='Choose', command=choose)
button_01.grid(row=1, column=0)
root.mainloop()
Upvotes: 1
Views: 101
Reputation: 49803
dict_01
is mapping names to the names of your images, as opposed to the images themselves. Remove the quotes around them, like so:
dict_01 = {'eins':dummy_logo,
'zwei':dummy_logo,
'drei':dummy_logo2,
'vier':dummy_logo2}
Upvotes: 2