Mar-20-2025, 10:56 AM
I am having a problem getting a radiobutton selection group to display correctly when I place it inside its own frame. This basic code works fine, with the first item selected as the default:
Grateful if someone could advise me what I need to do to get the radio buttons to open properly.
import tkinter as tk root = tk.Tk() countries = ["USA", "France", "Germany", "Sweden", "Brazil"] variable = tk.StringVar(root, f"{countries[0]}") for country in countries: tk.Radiobutton( root, text=country, variable=variable, value=country, ).pack(anchor="w", padx=10) root.mainloop()However, I want the radio button group to appear in a later part of the programme, so I put it in its own frame as in this code:
import tkinter as tk root = tk.Tk() def radios(): frm_radios = tk.Frame(root) countries = ["USA", "France", "Germany", "Sweden", "Brazil"] variable = tk.StringVar(frm_radios, f"{countries[0]}") for country in countries: tk.Radiobutton( frm_radios, text=country, variable=variable, value=country, ).pack(anchor="w", padx=10) frm_radios.pack() frm_btn = tk.Frame(root) clickbutton = tk.Button(master=frm_btn, text="Click here to show radiobuttons",command=radios) clickbutton.pack() frm_btn.pack() root.mainloop()When the radio buttons appear there is no item selected by default, and when you move your cursor into the area all the buttons except the first suddenly get selected.
Grateful if someone could advise me what I need to do to get the radio buttons to open properly.