2

I have an entry field in my tkinter GUI app. entry should only accept numbers including decimal points. I used a function to validate the entry. The problem is, it is not accepting decimal point(.) if there is a digit in front of it. (example 25.3 wont accept). if there is a point in the beginning, it is not accepting any number after that. Could anyone help me with this problem. and any suggestion to limit the maximum value in the entry field to 1000?

import tkinter as tk

def acceptNumber(inp):
    if inp.isdigit():
        return True
    elif inp is "":
        return True
    elif inp is ".":
        return True
    else:
        return False
win = tk.Tk()
reg = win.register(acceptNumber)

entryHere =tk.Entry(win)
entryHere.place(x=400, y=200)

entryHere.config(validate="key", validatecommand =(reg, '%P'))
win.mainloop()
master_yoda
  • 463
  • 3
  • 11

2 Answers2

1

This accepts valid decimal numbers not greater than 1000:

def acceptNumber(inp):
    try:
        return True if inp == '' else float(inp) <= 1000
    except:
        return False
Stef
  • 28,728
  • 2
  • 24
  • 52
0
>>> s='1234'
>>> s.isdigit()
True
>>> sdot = '1234.'
>>> sdot.isdigit()
False

Isn't this your problem. isdigit() means digits only.

Mike O'Connor
  • 2,494
  • 1
  • 15
  • 17