I have been following the validation for Entry boxes from here. The code below is from the answer with the added condition that if the entered value is 'Q'
then the program adds 'test'
to the beginning of the Entry value.
However once this value is inserted, all validation appears to go out of the window and the Entry allows upper-case values. Some testing on my program shows the validation command (OnValidate
in this case) isn't being called upon any further events of the Entry (key, focusin/out etc).
import Tkinter as tk
class MyApp():
def __init__(self):
self.root = tk.Tk()
vcmd = (self.root.register(self.OnValidate),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
self.entry = tk.Entry(self.root, validate="key",
validatecommand=vcmd)
self.entry.pack()
self.root.mainloop()
def OnValidate(self, d, i, P, s, S, v, V, W):
if S == "Q":
self.entry.insert(0,"test")
# only allow if the string is lowercase
return (S.lower() == S)
app=MyApp()
The reason for me doing this is I want the Entry to display a default value if its value is left empty after any changes that are made by a user. (i.e. my condition would be if not P
on focusout)
Any ideas how to implement this or what's going wrong in the above much appreciated.