Home > Mobile >  Change tkinter menu radiobutton indicator
Change tkinter menu radiobutton indicator

Time:12-20

Consider the menu below:

menu = tk.Menu(menubar, tearoff=0)
menu.add_radiobutton(label='A', variable=flavour_value, value='a')
menu.add_radiobutton(label='B', variable=flavour_value, value='b')

When radiobutton is selected, it shows V (checked) indicator from the left of a label.
Is there a possibility to replace the V indicator with a circular one, like in regular tk.Radiobutton?

CodePudding user response:

You can provide any indicators you want, by setting the image and selectimage options to the image of your choice. The image is for an unselected button, and selectimage is for a selected button.

CodePudding user response:

Apparently, Tkinter is not able to replace the 'check' indicator. The only way is to hide it with indicatoron=False, as was correctly mentioned by @acw1668.

Adding an image (image=...) will place it in a wrong position. The x0 (aka left) of an image will be just where the text usually begins.

Below is a small working example:

img = Image.open('wave.png')
tkimg = ImageTk.PhotoImage(img)

menu = tk.Menu(menubar, tearoff=0)
menu.add_radiobutton(label='A', variable=flavour_value, value='a', image=tkimg, compound='left', indicatoron=False)
menu.add_radiobutton(label='B', variable=flavour_value, value='b')
setattr(menu, 'img_a', tkimg)

The last line is needed to protect tkimg from garbage collection.

The result:
enter image description here

  • Related