Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 546 Bytes

allow-negative-integers.md

File metadata and controls

28 lines (21 loc) · 546 Bytes
title description author tags
Allow Negative Integers
A validation function to allow only negative integers.
Legopitstop
validation,negative,integers
from tkinter import Tk, Entry


def allow_negative_integers(value):
    return (
        value in ("", "-") or value.startswith("-") and value[1:].isdigit()
        if value
        else True
    )


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

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

root.mainloop()