My Radiobutton is checked on creation

My problem is that when creating a Radiobutton it is automatically checked and i can't uncheck it. I create it inside a frame of x and y dimensions. I've tried the .deselect() function but it changes nothing (Python 3.6)


frm = ttk.Frame(root),y=0,width=1000,height=1000)

Ek = ttk.Radiobutton(frm,text="text"),y=400)

And photo of it: photo

2 answers

  • answered 2018-01-13 17:28 Bryan Oakley

    Radiobuttons need to be associated with one of the special Tkinter variables (StringVar, etc), and are designed to work in groups of two or more. If you don't specify a variable, one will be created for you. The default value of a Radiobutton is the empty string, which is also the default variable will be set to.

  • answered 2018-01-13 17:48 Jason Wilkes

    First, if we just wanted to modify your code to give us a single unchecked radio button all by itself, this would do the trick.

    from tkinter import Tk, IntVar, Radiobutton, mainloop, ttk
    root = Tk()
    frm = ttk.Frame(root) = 0, y = 0, width = 1000, height = 1000)
    v = IntVar()
    Ek = ttk.Radiobutton(frm, text = "text", variable = v, value = 1) = 100, y = 100)

    Aside from the boilerplate for setup at the beginning and end, the only thing we had to change in your original code was to add the arguments variable = v, value = 1 to the Radiobutton call.

    Those extra arguments don't really make sense in isolation, for the same reason that it doesn't generally make sense to have a single radio button. Once we add two of them, we can see what's going on a bit better.

    In the documentation @Stack posted (this thing), the first code sample looks like this:

    from Tkinter import *
    master = Tk()
    v = IntVar()
    Radiobutton(master, text="One", variable=v, value=1).pack(anchor=W)
    Radiobutton(master, text="Two", variable=v, value=2).pack(anchor=W)

    If we run that, we get two unchecked radio buttons by default. If we then change the value=1 part to value=0, the first radio button shows up checked, and if we change value=2 to value=0, the second radio button shows up checked. So value=0 seems to give us buttons that are checked by default, but we don't know why yet. Let's experiment a bit more.

    If we try to delete pieces in the new sample until we get back to something more closely resembling what you wrote originally, we can sort of see what happened. Deleting the value arguments entirely and running it like this:

    Radiobutton(master, text="One", variable=v).pack(anchor=W)
    Radiobutton(master, text="Two", variable=v).pack(anchor=W)

    leaves us with neither button checked by default, though then further deleting the variable arguments to make the code look like your original call:

    Radiobutton(master, text="One").pack(anchor=W)
    Radiobutton(master, text="Two").pack(anchor=W)

    gives us two buttons that are both checked by default, which gets us back to your original problem.

    Basically, we're running into various odd corner cases here because we just started fiddling with code and forgot what a radio button actually represents.

    What the concept of a radio button represents in the first place is the value of a variable. Not the entire variable, just one of the things it might be equal to. And the set of radio buttons itself, taken together, gives us a visual representation of a discrete variable: a thing that can be in 1 of N states.

    So the API for Radiobuttons, naturally, is asking us for some information like "what python variable do you want us to use to hold these values?" (that's roughly the variable keyword) and "what values do you want us to glue to each of these buttons behind the scenes to distinguish the different states?" (that's the value keyword).

    As expected, the code works best in the case above where the values were 1 and 2, because in that case the code is properly reflecting what a radio button actually is, conceptually. When we collide the values or set them to zero or leave them out entirely, things get a bit weird and less predictable because we're then dealing with the implementation details of the tkinter API, rather than with the simple concept of a radio button that the API is meant to implement.

    Laptop's about to die, so I'm gonna go ahead and hit send. Hope that wasn't too wordy. Good luck. :)