![]() |
Update Image on Button Click - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Update Image on Button Click (/thread-43494.html) |
Update Image on Button Click - the_muffin_man - Nov-04-2024 Hi, I have asked this before and searched online. The answers i am getting are just far beyond where I am up to. My code may not be the most efficient nor may it be pretty but i know I am close and with a tweak here or there - anyway i am reaching out. The premise is simple a hangman game that changes the image when a wrong letter is guessed. On the button click it has to update the label. Here is what i have so far. import tkinter from tkinter import messagebox window = tkinter.Tk() window.title("a title") window.geometry("400x600") window.configure(background = "green") def checkans(): if user_inp.get() in letters: tkinter.messagebox.showinfo("CONGRATULATIONS","You have guessed a correct letter") else: tkinter.messagebox.showinfo("INCORRECT","That letter is not in the word") photo1Label.config(image = photo2,bd=0) letters = ["h","o","m","e"] photo1 = tkinter.PhotoImage(file = "hang1.png") photo2 = tkinter.PhotoImage(file = "hang2.png") photo3 = tkinter.PhotoImage(file = "hang3.png") photo4 = tkinter.PhotoImage(file = "hang4.png") photo1Label = tkinter.Label(window, image = photo1,bd=0) photo1Label.image = photo1 photo1Label.place(x=100,y=210) user_inp = tkinter.Entry(window,width=15) user_inp.place(x=155,y=420) check_guess = tkinter.Button(window,width=12,text="check if correct",fg="black",bg="gold",command=checkans) check_guess.place(x=155,y=447) window.mainloop()I am figuring that if i can get the label config to update to something like this it might work but it doesnt seem to like it. photo1Label.config(image = photo+str(counter),bd=0) I have also tried having a helper variable. That also doesn't work. bob = photo1+counter photo1Label.config(image = bob,bd=0) Been tearing my hair out. Thanks in advance for reading and supporting RE: Update Image on Button Click - the_muffin_man - Nov-04-2024 (Nov-04-2024, 11:47 PM)the_muffin_man Wrote: Hi, RE: Update Image on Button Click - menator01 - Nov-05-2024 This works for me. Check the comments in the code import tkinter from tkinter import messagebox window = tkinter.Tk() window.title("a title") window.geometry("400x600") window.configure(background = "green") # Index needs to be 1 for next image index = 1 photo1 = tkinter.PhotoImage(file = "hang1.png") photo2 = tkinter.PhotoImage(file = "hang2.png") photo3 = tkinter.PhotoImage(file = "hang3.png") photo4 = tkinter.PhotoImage(file = "hang4.png") # Put all photos in a list photos = [photo1, photo2, photo3, photo4] def checkans(): # Called global to use in function / Could have passed as args global photos, index if user_inp.get() in letters: tkinter.messagebox.showinfo("CONGRATULATIONS","You have guessed a correct letter") else: tkinter.messagebox.showinfo("INCORRECT","That letter is not in the word") photo1Label.config(image = photos[index],bd=0) user_inp.delete(0, 'end') # If we get to the end of list reset if index >= len(photos): index = 0 # Increase index for next photo index += 1 letters = ["h","o","m","e"] photo1Label = tkinter.Label(window, image = photo1,bd=0) photo1Label.image = photo1 photo1Label.place(x=100,y=210) user_inp = tkinter.Entry(window,width=15) user_inp.place(x=155,y=420) check_guess = tkinter.Button(window,width=12,text="check if correct",fg="black",bg="gold",command=checkans) check_guess.place(x=155,y=447) window.mainloop() |