William V.
William V.

Reputation: 577

All Python Radiobuttons Filled in Even Though Using: radio_1.select?

When I run the following code all the Radiobuttons that I have created are filled in, even though I have called the difficulty1.select function. I was told in a book that you could call difficulty1.select and then only the radio button that you wanted to be selected would be selected.

Code:

# Import Statements
from tkinter import *
import turtle
import tkinter.messagebox as box

# Windows Statements
window = Tk()
window.title("Options - CraftClash - Windows - Version 0.0.3 BETA")

# Widgets
difficultytitle = Label(window, text = "\n\nDifficulty:")
difficultyframe = Frame(window)
difficulty = StringVar()
difficulty1 = Radiobutton(difficultyframe, text = 'Easy', variable = difficulty)
difficulty2 = Radiobutton(difficultyframe, text = 'Normal', variable = difficulty)
difficulty3 = Radiobutton(difficultyframe, text = 'Hard', variable = difficulty)
difficulty1.select()

# Pack Statements
difficultytitle.pack(side = TOP)
difficulty1.pack(side = LEFT)
difficulty2.pack(side = LEFT)
difficulty3.pack(side = LEFT)
difficultyframe.pack(side = TOP)

# Sustain Window
window.mainloop()

Thanks!

Upvotes: 0

Views: 1268

Answers (1)

Sun Bear
Sun Bear

Reputation: 8234

The .select() method is meant to select (or switch on) a radio button. Before using it, you need to also first understand how the radiobuttons operates on their own when you make variable = StringVar(). I recommend you read this webpage on radiobutton. Quoting a paragraph:

You might wonder, what state is a group of radiobuttons in when the control variable has never been set and the user has never clicked on them? Each control variable has a default value: 0 for an IntVar, 0.0 for a DoubleVar, and '' for a StringVar. If one of the radiobuttons has that value, that radiobutton will be set initially. If no radiobutton's value option matches the value of the variable, the radiobuttons will all appear to be cleared.

So in your code, values were not defined. Let's assume the 3 buttons each have different values such as "Monday", "Tuesday" & "Wednesday", respectively. Next assign some value to your StringVar variable called "difficulty". You need to use the .set() method on difficulty. Notice that if it's value is different to the radiobutton value, the radiobutton will not be selected. Next assuming you let difficulty = 'Tuesday', you will see difficulty2 is selected. Next if you add the command difficulty1.select(), you will see difficulty2&3 is deselected and 1 is selected. Make the following changes to your code and try it out.

# Widgets
difficultytitle = Label(window, text = "\n\nDifficulty:")
difficultyframe = Frame(window)
difficulty = StringVar()
difficulty.set('Tuesday')
difficulty1 = Radiobutton(difficultyframe, text = 'Easy', variable = difficulty,
                          value='Monday')
difficulty2 = Radiobutton(difficultyframe, text = 'Normal', variable = difficulty,
                          value='Tuesday')
difficulty3 = Radiobutton(difficultyframe, text = 'Hard', variable = difficulty,
                          value='Wednesday')
#difficulty1.select()

Upvotes: 2

Related Questions