My question is based on this post: (Interactively validating Entry widget content in tkinter). I want to check the input of my entry which is not allowed to be <= 500000. The following code works fine but doesn't give the user proper feedback. Is there a more elegant way to tell the user the entered value is not allowed?
import tkinter as tk
class MyEntry(tk.Frame):
"""frame for trigger settings of a Case tab"""
def __init__(self, parent):
tk.Frame.__init__(self, parent)
# for input checking
validate_5e4 = self.register(self.validate_int50k)
self.entry = tk.Entry(parent, textvariable=tk.StringVar(), validate="all",
validatecommand=(validate_5e4, "%d", "%P"))
self.entry.grid(row=0, column=0)
def validate_int50k(self, insert, string):
"""limits the entry field to only accept int <= 5e4 as input"""
if insert == "1":
if string.isdigit():
try:
var = float(string)
if var <= 50000:
return True
else:
return False
except ValueError:
self.bell()
return False
else:
self.bell()
return False
else:
return True
root = tk.Tk()
app = MyEntry(root)
app.mainloop()