QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Allow Specific Characters

A validation function to allow specific characters.

  • validation

Code

from tkinter import Tk, Entry


def allow_specific_characters(value, allowed_chars):
    return all(char in allowed_chars for char in value)

Usage

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

reg = root.register(allow_specific_characters)
allowed_chars = "0123456789ABCDEFabcdef"  # Hexadecimal characters
Entry(root, validate="key", validatecommand=(reg, "%P", allowed_chars)).pack()

root.mainloop()