I want to have a set of two radiobuttons "BBC" and "CNN" in a submenu named "Channel" in my GUI.
I tried to use the add_radiobutton method, but the radiobuttons appear under the submenu, while I want them to behave as attributes for the "Channel" submenu, rather than additional submenus of the "News" menu.
from tkinter import *
root = Tk()
root.title("main")
root.geometry("500x500")
MAIN_MENU = Menu(root)
root.config(menu=MAIN_MENU)
File_menu = Menu(MAIN_MENU, tearoff=0)
MAIN_MENU.add_cascade(label="News", menu=File_menu, underline=0)
File_menu.add_command(label="Channel")
File_menu.add_radiobutton(label="BBC")
File_menu.add_radiobutton(label="CNN")
Settings_menu = Menu(MAIN_MENU, tearoff=0)
MAIN_MENU.add_cascade(label="History", menu=Settings_menu, underline=3)
root.mainloop()
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 tk
root = 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.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 tk
root = 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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With