Reputation: 77
In this program, I am attempting to check if an input matches a certain word (in this case 'APPLE') however the vcmd doesn't seem to be recognised - any ideas?
from tkinter import *
class Window(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.vmcd1 = parent.register(self.validate_entry)
self.title = Label(self, text='Enter here:')
self.title.pack()
self.entry = Entry(self, validatecommand=(self.vcmd1,'%P'))
self.entry.pack()
self.entered=Label(self, text='You entered')
self.entered.pack()
def callback(self):
self.entered.config(text='You entered: ' + self.entry.get())
def validate_entry(self, entry):
print('Code validates entry')
if entry == 'APPLE':
print('This input is correct.')
else:
pass
root = Tk()
frame = Window(root)
frame.pack()
root.mainloop()
Upvotes: 0
Views: 1679
Reputation: 16169
To summarize what is said in the comments:
There is a typo in the validate command name: you wrote self.vmcd1 = ...
instead of self.vcmd1 = ...
Your validate command should return True
(accept change) or False
(reject change). If I understand correctly what you want to achieve, your function should always return True
, but do something special if the content of the entry is 'APPLE'.
def validate_entry(self, entry):
print('Code validates entry')
if entry == 'APPLE':
print('This input is correct.')
return True
Your validate command is never executed because the default value for the validate
option is 'none'. You need to set this option to 'key' for instance if you want the content of the entry to be checked each time the user types something (more possible values here)
self.entry = Entry(self, validatecommand=(self.vcmd1, '%P'), validate='key')
Upvotes: 3