Make Radio Button on Menubar Dropdown selected by Default (tkinter Python)

Question:

I want the "Basic" option to be selected by default. How can I do this?

from tkinter import *
windowMenu = Menu(menubar,tearoff=0)
menubar.add_cascade(label="Window",menu=windowMenu)
windowMenu.add_radiobutton(label="Basic")
windowMenu.add_radiobutton(label="Extended")
Asked By: Canofeggs 5410

||

Answers:

You must associate a variable with the entries, define a value for each radiobutton, and then set the variable to the appropriate value.

from tkinter import *
root = Tk()
menubar = Menu(root)
root.configure(menu=menubar)

windowMenu = Menu(menubar,tearoff=0)
menubar.add_cascade(label="Window",menu=windowMenu)
var = StringVar(value="Basic")
windowMenu.add_radiobutton(label="Basic", variable=var)
windowMenu.add_radiobutton(label="Extended", variable=var)

root.mainloop()
Answered By: Bryan Oakley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.