I've been trying to implement validation in to an entryfield in tkinter so you can only enter integers: this is the code I have right now:
import tkinter as tk
class window2:
def __init__(self, master):
self.panel2 = tk.Frame(master)
self.panel2.grid()
self.button2 = tk.Button(self.panel2, text = "Quit", command = self.panel2.quit)
self.button2.grid()
self.entryfields()
def entryfields(self):
vcmd = (self.register(self.validate),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
self.text1 = tk.Entry(self.panel2, validate = 'key', validatecommand = vcmd)
self.text1.grid()
self.text1.focus()
def validate(self, action, index, value_if_allowed,
prior_value, text, validation_type, trigger_type, widget_name):
if(action=='1'):
if text in '0123456789.-+':
try:
float(value_if_allowed)
return True
except ValueError:
return False
else:
return False
else:
return True
if __name__ == "__main__":
root1 = tk.Tk()
window2(root1)
root1.mainloop()
Some of the code was taken from this forum as well. I think it's an inheritance issue which I'm missing out on.