I am making a tkinter
program where I want to restrict user input to only floats. I have done this using the register
and validate features. All of it is working fine but when I enter a set of numbers the program does not let me remove the first one. I think this has something to do with my final function inside my class, but I don't know how to edit it without breaking the rest of the program.
Here is my simplified problem.
from tkinter import *
class investables(Frame):
def __init__(self, master):
Frame.__init__(self, master=None)
self.pack()
self.master = master
vcmd = (master.register(self.validate),'%d', '%i', '%P', '%s',
'%S', '%v', '%V', '%W')
self.entry1 = Entry(self, validate="key", validatecommand=(vcmd))
self.entry1.grid(row=1, column=3)
# Define validate function, this also passes the many components
# of the register tool
def validate(self, d, i, P, s, S, v, V, W):
# Begin if else statement that prevents certain user input
if P:
try:
# Checks if %P is a float
float(P)
# If this is the case then the input is excepted
return True
# If this is not the case, then the code will not accept user
# input
except ValueError:
return False
# If any other input is inserted, the program will not accept it
# either
else:
return False
root = Tk()
my_gui = investables(master=root)
my_gui.mainloop()