12.07.2015 Views

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Part II: Connecting the interface to the rest of your programThe preceding sections talked about how to arrange and configure the widgets—the frontpanel of the application.Next, we’ll talk about how to connect up the widgets to the logic that carries out theactions that the user requests.20. Control variables: the values behind the widgetsA <strong>Tkinter</strong> control variable is a special object that acts like a regular <strong>Python</strong> variable in thatit is a container <strong>for</strong> a value, such as a number or string.One special quality of a control variable is that it can be shared by a number of differentwidgets, and the control variable can remember all the widgets that are currently sharingit. This means, in particular, that if your program stores a value v into a control variablec with its c.set(v) method, any widgets that are linked to that control variable areautomatically updated on the screen.<strong>Tkinter</strong> uses control variables <strong>for</strong> a number of important functions, <strong>for</strong> example: Checkbuttons use a control variable to hold the current state of the checkbutton (onor off). A single control variable is shared by a group of radiobuttons and can be used totell which one of them is currently set. When the user clicks on one radiobutton in agroup, the sharing of this control variable is the mechanism by which <strong>Tkinter</strong> groupsradiobuttons so that when you set one, any other set radiobutton in the group iscleared. Control variables hold text string <strong>for</strong> several applications. Normally the text displayedin an Entry widgets is linked to a control variable. In several other controls,it is possible to use a string-valued control variable to hold text such as the labels ofcheckbuttons and radiobuttons and the content of Label widgets.For example, you could link an Entry widget to a Label widget so that when theuser changes the text in the entry and presses the Enter key, the label is automaticallyupdated to show that same text.To get a control variable, use one of these four class constructors, depending on whattype of values you want to store in it:v = DoubleVar() # Holds a float; default value 0.0v = IntVar() # Holds an integer; default value 0v = StringVar() # Holds a string; default value ""All control variables have these two methods:.get()Returns the current value of the variable..set ( value )Changes the current value of the variable. If any widget options are slaved to this variable,those widgets will be updated when the main loop next idles; see .update_idletasks()New Mexico Tech Computer Center <strong>Tkinter</strong> <strong>reference</strong>: Control variables Page 73

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!