0

I have the following code (in python and tkinter):

c1 = tk.Radiobutton (frm, text='a',variable= radioValue ,value=1)
c2 = tk.Radiobutton (frm, text='b',variable= radioValue ,value=2)
c3 = tk.Radiobutton (frm, text='c',variable= radioValue ,value=3)
c4 = tk.Radiobutton (frm, text='d',variable= radioValue ,value=4)

then I retrieve a value called r from database, it is between 1 and 4.

I use r to set a radio button to on by using many if statements, and it works well. The if statements are:

if (r==1): 
    (c1.select())
if (r==2): 
    (c2.select())
if (r==3): 
    (c3.select())
if (r==4): 
    (c4.select())

Is there a better solution instead of multiple if?

3
  • You can save your radio button in a dict with key referring to the radio button of its value then later use get method of the dict. Commented May 27, 2021 at 6:46
  • Use radioValue.set(r). Commented May 27, 2021 at 6:51
  • use elif after if. no need to check rest of them Commented May 27, 2021 at 6:51

1 Answer 1

2

You can simply use the tkinter variable radioValue to set the active radiobutton:

radioValue.set(r)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.