maxlenght in Entry() / Tkinter

Fredrik Lundh fredrik at pythonware.com
Mon Jul 15 03:16:23 EDT 2002


Joaquin Romero wrote:

> I can setting a maxlength (max numebers of characters) in a Entry() of
> Tkinter? How?

matthew showed you how to use an event binding to
reject keyboard presses when the value is already long
enough.

another approach is illustrated by the ValidatingEntry
class available from:

http://effbot.org/zone/tkinter-entry-validate.htm

to make a version that limits the length, just check the
length in the validate method; e.g.

class MaxLengthEntry(ValidatingEntry):

    def __init__(self, master, value, maxlength, **kw):
        self.maxlength = maxlength
        apply(ValidatingEntry.__init__, (self, master), kw)

    def validate(self, value):
        if len(value) <= self.maxlength:
            return value
        return None # new value too long

and here's how to use the class:

    root = Tk()

    entry = MaxLengthEntry(root, "", 20)
    entry.pack()

    mainloop()

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list