Three Tkinter questions

richard_chamberlain richard_chamberlain at ntlworld.com
Mon Jun 19 14:24:57 EDT 2000


Hi Egbert,

> When and why should i use Intvar() etc ?
> When and how do i use the textvariable option ?

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.

> When do you need mainloop() more than once ?

mainloop() starts the event loop up, I wouldn't use it more than once.

> Three questions only:

Three questions is quite a lot for one post ;)

Richard

Egbert Bouwman <egbert at bork.demon.nl> wrote in message
news:20000619140251.A6860 at bork...
> Three questions only:
>
> When and why should i use Intvar() etc ?
> When do you need mainloop() more than once ?
> When and how do i use the textvariable option ?
> Examples are very welcome.
> egbert
> --
> Egbert Bouwman - Keizersgracht 197 II - 1016 DS  Amsterdam - 020 6257991
> ========================================================================
>





More information about the Python-list mailing list