Home > Blockchain >  Why do all the radiobuttons start selected, and how do I stop that?
Why do all the radiobuttons start selected, and how do I stop that?

Time:02-19

Below is my code. i would like to know how to start the program with the radio buttons not selected.

from tkinter import *
    
    
root = Tk()
    
root.title("Early Learning Tool")
root.geometry("800x400")
    
frame = LabelFrame(root, text="Letters", padx=30, pady=10)
frame.pack()
    
photo_A = PhotoImage(file=r"Pictures\A.png")
photo_B = PhotoImage(file=r"Pictures\B.png")
photo_C = PhotoImage(file=r"Pictures\C.png")
photo_D = PhotoImage(file=r"Pictures\D.png")
photo_E = PhotoImage(file=r"Pictures\E.png")
    
    
letter_choice = StringVar()
    
rb_A = Radiobutton(frame, variable=letter_choice, value="A", image=photo_A)
rb_A.grid(row=0, column=0)
rb_B = Radiobutton(frame, variable=letter_choice, value="B", image=photo_B)
rb_B.grid(row=0, column=1)
rb_C = Radiobutton(frame, variable=letter_choice, value="C", image=photo_C)
rb_C.grid(row=0, column=2)
rb_D = Radiobutton(frame, variable=letter_choice, value="D", image=photo_D)
rb_D.grid(row=0, column=3)
rb_E = Radiobutton(frame, variable=letter_choice, value="E", image=photo_E)
rb_E.grid(row=0, column=4)
    
root.mainloop()

This is what appears. How do I make it so it doesn't do that? enter image description here

CodePudding user response:

In letter_choice variable you have to set a default value in StringVar()

like so:

letter_choice = StringVar(value="A")

CodePudding user response:

I tried running the programming switching the images to text.

from tkinter import *
    
    
root = Tk()
    
root.title("Early Learning Tool")
root.geometry("800x400")
    
frame = LabelFrame(root, text="Letters", padx=30, pady=10)
frame.pack()

letter_choice = StringVar()
    
rb_A = Radiobutton(frame, variable=letter_choice, value="A", text="A")
rb_A.grid(row=0, column=0)
rb_B = Radiobutton(frame, variable=letter_choice, value="B", text="B")
rb_B.grid(row=0, column=1)
rb_C = Radiobutton(frame, variable=letter_choice, value="C", text="C")
rb_C.grid(row=0, column=2)
rb_D = Radiobutton(frame, variable=letter_choice, value="D", text="D")
rb_D.grid(row=0, column=3)
rb_E = Radiobutton(frame, variable=letter_choice, value="E", text="E")
rb_E.grid(row=0, column=4)
    
root.mainloop()

I didn't encounter any problems. Indeed, the default radiobutton when no buttons are clicked, appears with a small grey dot. But when clicking on a radiobutton the default grey dots disappear.

CodePudding user response:

You need to initialize the StringVar with value other than empty string (default value if not specified) or the values for those radiobuttons, for example ' ':

letter_choice = StringVar(value=' ')
  • Related