faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 2 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

IntVar - to detect a change in it

Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 147, Frank Niessink


"""
Packages: gui.tkinter
"""
"""
> Is there anyway an altering of a variable (Int-, String- or Double-)
> (by the user, via the GUI) could call a function in order to tell the
> environment that it has changed since the initial setting(or a static
> comparison value)
Assuming you're talking about Tkinter here: below's an example of using
trace() to trace a variable:
"""
from Tkinter import Label
class VLabel(Label):
    """ VLabel class - becomes red when textvariable.valid() returns
    false """
    def __init__(self, master, textvariable, **cnf):
        apply(Label.__init__, (self, master), cnf)
        self.config(textvariable=textvariable)
        self.textvariable = textvariable
        textvariable.trace('w',self.notify)
    def notify(self, *args):
        if self.textvariable.valid():
            self.config(bg=self.config('bg')[3])
        else:
            self.config(bg='red')
"""
Everytime the textvariable is changed notify() gets called, which checks
whether the textvariable is valid. If not, the background is colored
red,
if it is valid, the background is set to its default color.
"""