Entry
When and why should I use Intvar() etc?
When and how do I use the textvariable option?
Jun 20th, 2000 04:51
unknown unknown, richard_chamberlain
from Tkinter import *
root=Tk()
myIntVar = IntVar()
myStringVar= StringVar()
Label(root,textvariable=myStringVar).pack(side=TOP)
def setLabel():
myStringVar.set(myIntVar.get())
Radiobutton(root,text="First Radiobutton",variable=myIntVar,value=1,
command=setLabel).pack(side=LEFT)
Radiobutton(root,text="Second Radiobutton",variable=myIntVar,value=2,
command=setLabel).pack(side=LEFT)
root.mainloop()
I'm using both IntVar and textvariable in this example.
I create new instances of Intvar and StringVar. I then create a label
and assign the textvariable option to the instance of StringVar. I can
now set the text of this label by called myStringVar.set('Hello') or get
the text by myStringVar.get().
Next I've created two Radiobuttons and assigned their variable options
to the instance of IntVar. I give them two separate values. Any changes
to the radiobuttons will now be reflected in myIntVar.
I created a function called setLabel which uses the set and get methods
of the instances. I then assign it to the label by called
myStringVar.set(myIntVar.get()) which changes the value of the
textvariable and the change is immediately reflected on the label.