0

I am working on a script with tkinter, but something weird is happening.

I have two radioButtons:

way=False
RadioButton0=Radiobutton(root,text="From",variable=way,value=False)
RadioButton1=Radiobutton(root,text="To",variable=way,value=True)
RadioButton0.grid(column=0,row=2)
RadioButton1.grid(column=1,row=2)

And a text entry field:

entryValue=0
entryField=Entry(root,textvariable=entryValue)
entryField.grid(column=0,row=4)

When I enter 0 in entry field, RadioButton0 is automatically selected, when I enter 1, RadioButton1 is selected and for any other value, they both get selected... This works vice versa: when I select RadioButton0, entry field changes to 0 and when I select RadioButton1, entry field changes to 1... Also, entryValue is later seen as 0. Variable way should only be modified by radio buttons...

Why is that happening? Am I doing something I shouldn't? And how do I fix it?

2 Answers 2

3

variable and textvariable should be both different variable objects, not just built-in data types:

way=BooleanVar(root)
way.set(False)
# ...
entryValue=StringVar(root)
entryValue.set("0")
Sign up to request clarification or add additional context in comments.

Comments

2

you can use a command to call a method and set the value. Please refer attached code.

def sel():
   selection = "You selected the option " + str(var.get())
   label.config(text = selection)


root = Tk()
frame = Frame(root)
frame.pack()

labelframe = LabelFrame(frame, text="This is a LabelFrame")
labelframe.pack(fill="both", expand="yes")


var = IntVar()
R1 = Radiobutton(labelframe, text="Option 1", variable=var, value=1,
                  command=sel)
R1.pack( anchor = W )

R2 = Radiobutton(labelframe, text="Option 2", variable=var, value=2,
                  command=sel)
R2.pack( anchor = W )

R3 = Radiobutton(labelframe, text="Option 3", variable=var, value=3,
                  command=sel)
R3.pack( anchor = W)


label = Label(labelframe)
label.pack()

1 Comment

Does this example really answer the question?

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.