What is intvar?

Rhodri James rhodri at wildebst.demon.co.uk
Thu Jan 22 22:00:45 EST 2009


On Fri, 23 Jan 2009 02:38:32 -0000, W. eWatson <notvalid2 at sbcglobal.net>  
wrote:

> I'm looking at someone's code in which invar() is used fairly often.  
> Apparently, it's a Tkinter method. Here's a use:
>      def body(self,master):
>          self.title("Display Settings")
>
>          self.colorVar = IntVar()
>          Radiobutton( master, text="Gray Scale",
>                       value=1, variable=self.colorVar).grid(row=0,  
> sticky=W)
>          Radiobutton( master, text="Pseudo Color",
>                       value=2, variable=self.colorVar).grid(row=1,  
> sticky=W)
>          ...
>
> What is the need for this use? It looks like some sort of initialization  
> for a widget.

Chris has already pointed you to the Tkinter documentation, which is a
good start but a little less than clear in places.

What your example code does is to associate self.colorVar with the set
of radio buttons.  This isn't just initialisation, it's a full reflection
of the current state.

self.colorVar.get() returns the "value" parameter that you gave to the
radio button which is currently selected (so, for example, if it returns
1 you know that the "Grey Scale" button is the one selected).  Similarly
self.colorVar.set(n) selects whichever radio button has the "value" which
matches n (so self.colorVar.set(2) selects the "Pseudo Color" button).

IntVar and StringVar (and DoubleVar if you must :-) are basically the
easy way of dynamically reading and changing the values in various
entry widgets, the text on buttons, and so on.

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list