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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

in the section on Universal widget methods, above, <strong>for</strong> more in<strong>for</strong>mation on controlling thisupdate cycle.Here are some comments on how control variables are used with specific widgets: Button: You can set its textvariable to a StringVar. Anytime that variable ischanged, the text on the button will be updated to display the new value. This is notnecessary unless the button’s text is actually going to change: use the text attributeif the button’s label is static. Checkbutton: Normally, you will set the widget’s variable option to an IntVar,and that variable will be set to 1 when the checkbutton is turned on and to 0 whenit is turned off. However, you can pick different values <strong>for</strong> those two states with theonvalue and offvalue options, respectively. You can even use a StringVar as thecheckbutton’s variable, and supply string values <strong>for</strong> the offvalue and onvalue.Here’s an example:self.spamVar = StringVar()self.spamCB = Checkbutton ( self, text="Spam?",variable=self.spamVar, onvalue="yes", offvalue="no" )If this checkbutton is on, self.spamVar.get() will return the string "yes"; if thecheckbutton is off, that same call will return the string "no". Furthermore, yourprogram can turn the checkbutton on by calling .set("yes").You can also the textvariable option of a checkbutton to a StringVar. Thenyou can change the text label on that checkbutton using the .set() method on thatvariable. Entry: Set its textvariable option to a StringVar. Use that variable’s .get()method to retrieve the text currently displayed in the widget. You can also thevariable’s .set() method to change the text displayed in the widget. Label: You can set its textvariable option to a StringVar. Then any call tothe variable’s .set() method will change the text displayed on the label. This isnot necessary if the label’s text is static; use the text attribute <strong>for</strong> labels that don’tchange while the application is running. Menubutton: If you want to be able to change the text displayed on the menubutton, set its textvariable option to a StringVar() and use that variable’s.set() method to change the displayed text. Radiobutton: The variable option must be set to a control variable, either anIntVar or a StringVar. All the radiobuttons in a functional group must share thesame control variable. Set the value option of each radiobutton in the group to adifferent value. Whenever the user sets a radiobutton, the variable will be set tothe value option of that radiobutton, and all the other radiobuttons that share thegroup will be cleared.You might wonder, what state is a group of radiobuttons in when the control variablehas never been set and the user has never clicked on them? Each control variablehas a default value: 0 <strong>for</strong> an IntVar, 0.0 <strong>for</strong> a DoubleVar, and "" <strong>for</strong> a StringVar.If one of the radiobuttons has that value, that radiobutton will be set initially. If noradiobutton’s value option matches the value of the variable, the radiobuttons willall appear to be cleared.New Mexico Tech Computer Center <strong>Tkinter</strong> <strong>reference</strong>: Control variables Page 74

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

Saved successfully!

Ooh no, something went wrong!