Python Forum
tkinter Radio button group not setting default selection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter Radio button group not setting default selection
#1
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.
Reply
#2
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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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?
Reply
#4
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using place for a button, many examples but AttributeError, tkinter Edward_ 3 922 Dec-17-2024, 09:06 PM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 4,482 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 2,419 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 13,809 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Is there a way to determine if a radio button has been selected? TWB 5 10,104 Jan-31-2023, 09:44 AM
Last Post: Vadanane
  Tkinter - How can I change the default Notebook border color? TurboC 5 17,411 May-23-2022, 03:44 PM
Last Post: bigmac
  Can't get tkinter button to change color based on changes in data dford 4 4,791 Feb-13-2022, 01:57 PM
Last Post: dford
  [Tkinter] Radio Buttons Working Bassackwards gw1500se 6 3,389 Dec-07-2021, 07:13 PM
Last Post: menator01
  [Tkinter] Grid the radio buttons Joni_Engr 6 7,296 Nov-24-2021, 07:20 PM
Last Post: menator01
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 7,639 Oct-09-2021, 07:30 AM
Last Post: cybertooth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020