How to add radiobuttons to a submenu in Tkinter How to add radiobuttons to a submenu in Tkinter tkinter tkinter

How to add radiobuttons to a submenu in Tkinter


You must make a submenu first, add the radiobuttons to it, and then add it as a cascade to your main menu. Then, add that menu to your menu bar.

menuBar = tk.Menu(root)menu1 = tk.Menu(root)submenu = tk.Menu(root)submenu.add_radiobutton(label="Option 1")submenu.add_radiobutton(label="Option 2")menuBar.add_cascade(label="Menu 1", menu=menu1)menu1.add_cascade(label="Subemnu with radio buttons", menu=submenu)

Full working example:

import tkinter as tkroot = tk.Tk()menuBar = tk.Menu(root)menu1 = tk.Menu(root)submenu = tk.Menu(root)submenu.add_radiobutton(label="Option 1")submenu.add_radiobutton(label="Option 2")menuBar.add_cascade(label="Menu 1", menu=menu1)menu1.add_cascade(label="Subemnu with radio buttons", menu=submenu)root.config(menu=menuBar)root.mainloop()

You'll probably want to add some attributes to your radiobuttons.A more complete form would be:

add_radiobutton(label="Option 1", value=1, variable=optionVar, command=on_option_1)

Where:

  • label is the text that appears in the menu;
  • variable is a tk.Variable instance, generally an IntVar or a StringVar;
  • value is the value to set to variable when the option is selected;
  • command is the callback to be run when the option is selected.