Question

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?

Was it helpful?

Solution 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()

OTHER TIPS

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")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top