Michael Onfrek wrote:

> import Tkinter as tk
> 
> Hi! Can you explain what line above mean?
> 
> I also found : http://effbot.org/zone/tkinter-entry-validate.htm
> 
> It works for me, but I not really  understand how? :)

>> import Tkinter as tk

Make objects defined in Tkinter available under the tk prefix.
E. g. to access an Entry you can do 'tk.Entry'. Had you imported it 
'import Tkinter' you would have to do 'Tkinter.Entry' instead. So you
are saving a few keystrokes. Doing 'from Tkinter import *' saves you still
more keystrokes but is considered bad style except for demonstration
purposes.

>> var = tk.StringVar()
>> entry = tk.Entry(root, textvariable=var)

Create a StringVar and connect it to the Entry widget. Any changes the user
makes in the Entry are reflected in the StringVar's value which can be
accessed with its get() method.

>> max_len = 5
>> def on_write(*args):
>>     s = var.get()
>>     if len(s) > max_len:
>>         var.set(s[:max_len])

Define a function that doesn't care about the arguments passed to it. It 
reads the current value of the StringVar 'var' and, if necessary, trims it
to 'max_len_' characters.

>> var.trace_variable("w", on_write)

Tell the StringVar to call the function on_write() every time its value is
changed. So every time the user edits the data in the Entry, in turn the
Entry changes the data of the StringVar, which calls the on_write()
function which may or may not change the StringVar -- and that change is
reflected in what the Entry displays. This smells like an endless loop, but
so far we seem to be lucky...

If you look again at Fredrik Lundh's ValidatingEntry, you will find all the
elements explained above packed nicely into one class, with the extra
refinement that he keeps another copy of the value which is used to restore
the old state when the new value is found to be invalid.

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to