Radio Buttons won't select jpg Radio Buttons won't select jpg tkinter tkinter

Radio Buttons won't select jpg


I removed most of the code not relevant to the question and changed the images to ordinary PhotoImages for simplicity. Also I changed the first positioning of the image on the canvas.

If you want to assign a value to a variable inside a function you'll have to make it global or it will not work. The variable will be defined in the local function scope and will be garbage collected when the function ends.

I don't think you can change an image on a canvas by updating the variable you used to create it. That's how a StringVar functions.

As the image is the only widget on the canvas I delete ALL items and then create a new image when I toggle images.

Also: I use Python 3.6 so I spell tkinter without the capital T.

from tkinter import *    root = Tk() #load 2 images for stampingtkimg = PhotoImage(file='test.gif')     # Test imagetkimg2 = PhotoImage(file='tesu.gif')    # Test image# A Radiobutton to toggle between imagesv = IntVar()def call():    canvas.delete(ALL)    if v.get() == 1:        canvas.create_image((2, 2), image=tkimg, anchor=NW)    else:        canvas.create_image((2, 2), image=tkimg2, anchor=NW)Label(root, text ="Select an image to place.").grid(row=1, column=0, columnspan=5, sticky=S)R1=Radiobutton(root, text="Bird 1", variable=v, value=1, command=call)R1.grid(row=2, column=0, sticky=N+E)R1.select()R2=Radiobutton(root, text="Bird 2", variable=v, value=2, command=call)R2.grid(row=2, column=1, sticky=N+E)# A canvas for mouse events and image drawingcanvas = Canvas(root, height=200, width=200, bg='#2EEAFF')canvas.grid(column=5, row=0, rowspan=4, sticky=W)canvas.create_image((2, 2), image=tkimg, anchor=NW)# Enter event looproot.mainloop()