Python Forum
Update Image on Button Click
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update Image on Button Click
#1
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
Reply
#2
(Nov-04-2024, 11:47 PM)the_muffin_man Wrote: 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

Attached Files

Thumbnail(s)
               
Reply
#3
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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to update label text using a grid button. Edward_ 7 1,508 Dec-18-2024, 03:05 AM
Last Post: Edward_
  [Tkinter] button image changing but not visually updating RuaridhW 2 825 Dec-16-2024, 03:18 PM
Last Post: RuaridhW
  tkinter - touchscreen, push the button like click the mouse John64 5 2,421 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Figure Gets Larger Every time I click a show button joshuagreineder 2 2,090 Aug-11-2022, 06:25 AM
Last Post: chinky
  [Tkinter] Why does the image for the button not appear? finndude 4 3,183 Oct-21-2021, 06:41 PM
Last Post: deanhystad
  [Tkinter] Make my button text update? Skata100 1 2,703 Aug-07-2021, 05:37 AM
Last Post: deanhystad
  [Tkinter] image inside button rwahdan 4 11,038 Jul-12-2021, 08:49 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 6,910 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  [Tkinter] Modify Class on Button Click KDog 4 5,148 May-11-2021, 08:43 PM
Last Post: KDog
  tkinter button image Nick_tkinter 4 5,336 Mar-04-2021, 11:33 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020