'Isn't it 'save' to pass an argument as an option to a radio button?

First of all, thank you for reading my problem.

I wanted to create a program that creates a photo print button when a desired animal is selected with a radio button in the Windows interface, and prints a photo with the button.

It seems that the value is stored in the integer variable var by the radio button option, so I called vote_animal, the photo output button function first.

def vote_animal():
    button1 = Button(window, text = "Picture", font = (5), fg = "black", command = show_animal)
    button1.place(x = xPos, y = yPos)

#main code

var = IntVar()
dogRB = Radiobutton(window, text = "Dog", variable = var, value = 1, command = vote_animal) 
catRB = Radiobutton(window, text = "Cat", variable = var, value = 2, command = vote_animal)
rabbitRB = Radiobutton(window, text = "Rabbit", variable = var, value = 3, command = vote_animal)

dogRB.pack()
catRB.pack()
rabbitRB.pack()

Then I believed that when the print photo button was clicked, the show_animal function would be called and the function would print the picture according to the var variable.

def show_animal():
    if var.get() == 1:
        photo = PhotoImage(file = "C:/picture/"+animalList[var.get()-1])
        label2.configure(image = photo)
        label2.pack()
    
    elif var.get() == 2:
        photo = PhotoImage(file = "C:/picture/"+animalList[var.get()-1])
        label2.configure(image = photo)
        label2.pack()
    
    else :
        photo = PhotoImage(file = "C:/picture/"+animalList[var.get()-1])
        label2.configure(image = photo)
        label2.pack()

Oh, for reference, the last path of the photo is implemented as animalList.

animalList=["dog.gif", "cat.gif", "rabbit.gif"]

Anyway, if I make the code like this, the picture is not printed even though there are no errors. So, I want to ask if the value of var does not apply to the show_animal function, or if there is another problem.

In case you don't know, I've put the code on github. Github URL: https://github.com/baecci/For_Stackoverflow

The first question may be a bit messy, but I would appreciate your kind answer.



Solution 1:[1]

The display of images is fine, but there are shortcomings in the program.

I recommend these modifications in the code on GitHub:

  1. change line 55 to: photo = PhotoImage(file = "C:/picture/"+animalList[0])
  2. insert per line 56: label2.photo_ref = photo (see details How to update the image of a Tkinter Label widget?)
  3. insert next line: label2.place(x = 10, y = 200) (or another location or pack())
  4. after line with label2.configure insert line label2.photo_ref = photo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Finke