Python Forum

Full Version: tkinter Radio button group not setting default selection
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

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.
Check https://stackoverflow.com/q/5071559/4046632

Here I just moved frame and var out of the function. Better create a class.
import tkinter as tk
 
root = tk.Tk()
frm_radios = tk.Frame(root)
variable = tk.StringVar(frm_radios)

def radios():
     
    countries = ["USA", "France", "Germany", "Sweden", "Brazil"]

    for country in countries:
        rb = tk.Radiobutton(
            frm_radios,
            text=country,
            variable=variable,
            value=country,
        )
        rb.pack(anchor="w", padx=10)
 
    frm_radios.pack()
    variable.set(f"{countries[0]}")
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()
That's brilliant. Thanks very much.

Thanks also for the link to stackexchange which provides a lot of intersting background on what causes this problem. I did try a search on stackexchange but I suppose my search wasn't specific enough, and I didn't mange to find anything which was relevant.

I'm not sure how to mark your post as an Answer?
(Mar-20-2025, 05:47 PM)simonc88 Wrote: [ -> ]I'm not sure how to mark your post as an Answer?
This is how SO works. here you don't have to mark/accept answer. It's a discussion forum, not Q&A site like SO