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()
########################################

Regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

There is a multi-legged creature crawling on your shoulder.
                -- Spock, "A Taste of Armageddon", stardate 3193.9
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to