0

I have a problem with the three radio buttons, which is that they accept all of them to be selected at once. How do I prevent this thing, and when I press the other button, the selection goes back from that first one?

    pen_size_1 = customtkinter.CTkRadioButton(pen_size_frame, 
                                        text="Small", 
                                        value=3, 
                                        command=lambda: change_size(3),
                                        state="normal",
                                        fg_color="#ad85b9",
                                        hover_color= "#a655be",
                                        )
pen_size_1.pack(side="left")


pen_size_2 = customtkinter.CTkRadioButton(pen_size_frame, 
                                        text="Medium", 
                                        value=5, 
                                        command=lambda: change_size(5), 
                                        fg_color="#ad85b9",
                                        hover_color= "#a655be",
                                        
                                        )
pen_size_2.pack(side="left")

pen_size_3 = customtkinter.CTkRadioButton(pen_size_frame, 
                                        text="Large", 
                                        value=7, 
                                        command=lambda: change_size(7), 
                                        state="normal",
                                        fg_color="#ad85b9",
                                        hover_color= "#a655be"
                                        )
pen_size_3.pack(side="left")
1
  • 4
    You need to assign the variable option of these buttons to same tkinter variable.
    – acw1668
    Sep 27 at 4:04

1 Answer 1

0

As already mentioned in the comments by acw1668 you have to introduce one IntVar for all the radiobuttons to store the value of the selected one. Further you can reduce the number of redundant lines of code by generating the radiobuttons from a loop:

pen_size_var = tkinter.IntVar(value=3)  # set start value here

pen_size_rbtn = []
for i, size in enumerate(['Small', 'Medium', 'Large'], 1):
    pen_size_rbtn.append(customtkinter.CTkRadioButton(pen_size_frame,
                                                      text=size,
                                                      value=i*2+1,
                                                      variable=pen_size_var,
                                                      state="normal",
                                                      fg_color="#ad85b9",
                                                      hover_color="#a655be",))
    pen_size_rbtn[-1].pack(side="left")

Whatever variable is set by your change_size() function can now be replaced by pen_size_var

2
  • Note that first argument of IntVar() is the master of the variable, not the value. Use value=3 instead. Please test your solution before posting.
    – acw1668
    Sep 27 at 11:30
  • You are absolutely right, I edited my post accordingly. Must have slipped my attention.
    – Jan_B
    Sep 27 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.