Skip to content

Files

Latest commit

7baca37 · Jan 22, 2025

History

History
32 lines (25 loc) · 589 Bytes

allow-decimal.md

File metadata and controls

32 lines (25 loc) · 589 Bytes
title description author tags
Allow Decimal
A validation function to allow only decimal numbers.
Legopitstop
validation,decimals
from tkinter import Tk, Entry


def allow_decimal(action, value):
    if action == "1":
        if value == "":
            return True
        try:
            float(value)
            return True
        except ValueError:
            return False
    return True


# Usage:
root = Tk()
root.geometry("200x200")

reg = root.register(allow_decimal)
Entry(root, validate="key", validatecommand=(reg, "%d", "%P")).pack()

root.mainloop()