0

I have written some code which takes a date of birth and calculates the correct Chinese horoscope animal (rat, dog etc.). I would like to add a gui whereby, when the user hits calculate, the gui shows a photo of the appropriate animal. To do that I think I have to make the file path for an image change within the command definition, but can't find a way to do this. I have simplified the code as far as possible below:

x = Tk()
x.state('zoomed')

def find_animal():
    animal = 'tiger' # I've left out the actual calculation part
    picture.place(relx=0.4, rely=0.5)

b = Button(x, text='Calculate', command=find_animal)
b.place(relx=0.5, rely=0.3)

file_path = 'rabbit.png'
pic = PhotoImage(file=file_path)
picture = Label(x, image=pic)

x.mainloop()

The thing I'm hoping to do is somehow alter the file_path variable within the find_animal function so that the image displayed changes from rabbit to tiger

1 Answers1

1

You can use pic.config() to change the image path inside find_animal():

def find_animal():
    animal = 'tiger' # I've left out the actual calculation part
    pic.config(file=animal+".png")
    picture.place(relx=0.4, rely=0.5)
acw1668
  • 40,144
  • 5
  • 22
  • 34