QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Restrict Length

A validation function to limit the length.

  • validation
  • length

Code

from tkinter import Tk, Entry


def restrict_length(value, max_length):
    return len(value) <= int(max_length)

Usage

root = Tk()
root.geometry("200x200")

reg = root.register(restrict_length)
# 10 is the maximum length allowed
Entry(root, validate="key", validatecommand=(reg, "%P", 10)).pack()

root.mainloop()