Skip to content

Tkinter ::: Radiobutton class is not working properly based on type of value supplied to it #101005

@sangram11

Description

@sangram11

In Tkinter package Radiobutton is not working properly. It is showing different behavior based on different type of value selected for a given variable.

from tkinter import *

root = Tk()
root.geometry("300x300")

gender = StringVar() #gender variable is set as string type
male = Radiobutton(root, text = "Male", variable = gender, value = "male")
female = Radiobutton(root, text = "Female", variable = gender, value = "female")

male.pack()
female.pack()

root.mainloop()

image

If you run this simple piece of code, you will find all radio buttons are selected.
This is happening because gender variable is set as string type.
A user obviously doesn't want all radio buttons to be selected.

Whereas if you run this below piece of code for integer type

from tkinter import *

root = Tk()
root.geometry("300x300")

gender = IntVar() #gender variable is set as integer type
male = Radiobutton(root, text = "Male", variable = gender, value = 1)
female = Radiobutton(root, text = "Female", variable = gender, value = 2)

male.pack()
female.pack()

root.mainloop()

image

You will find none of the option has been selected for radio buttons. This is the actual expectation for any user. They don't want all radio buttons to be selected.

Moreover, there should be an argument in Radiobutton class to select a particular radio button out of many. This is missing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-tkintertype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions