Hi Michael,

On Wed, 13 Jun 2012 20:43:24 +0200
Michael Lange <klappn...@web.de> wrote:
> Hi,
> 
> Thus spoketh Mark Summerfield <l...@qtrac.plus.com> 
> unto us on Wed, 13 Jun 2012 13:55:36 +0100:
> 
> > 
> > I didn't check "everything", but I tried it and it worked. I respects
> > the from and to values (when using the up and down arrows or the up and
> > down arrow keys), but just like tk.Spinbox allows you to type in any
> > old junk.
> 
> Sure, if you want only a certain set of values to be legal, you need to
> deal with validatecommand and friends, see
> 
> http://www.tcl.tk/man/tcl8.4/TkCmd/spinbox.htm#M26
> 
> Validation is not exactly trivial in Python though, because you will
> have to manage percent substitutions yourself, as in this little example
> of a spinbox that only accepts float values between -10.0 and 100.0:
> 
> ########################################
> from Tkinter import *
> 
> root = Tk()
> var = StringVar()
> var.set('0')
> s = Spinbox(root, from_=-10.0, to=100.0, increment=1, textvariable=var,
>             validate='all', format='%0.1f')
> s.pack()
> 
> def validate(old, new):
>     if new == '':
>         res = True
>     else:
>         try:
>             x = float(new)
>             if float(s['from']) <= x <= float(s['to']):
>                 res = True
>             else:
>                 res = False
>         except ValueError:
>             res = False
>     return res
> 
> vcmd = (s.register(validate), '%s', '%P')
> s.config(vcmd=vcmd, invcmd=s.bell)
> 
> root.mainloop()
> ########################################

Wow! I hadn't known about the register() method.
AFAIK it isn't in the Tcl/Tk or Tkinter documentation.
(Which makes me wonder what else is missing?)

Incidentally, in the example above, the '%s' and old value are
redundant.


Thanks:-)


-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Programming in Go" - ISBN 0321774639
            http://www.qtrac.eu/gobook.html
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to