Reputation: 565
I have a set of entry boxes created in a for loop and recorded in a dictionary that I want to validate independently. The loop that creates them follows:
vcmd = (self.register(self.VE), '%P')
for pos in [(i,j) for i in range(9) for j in range(9)]:
self.inputDict[pos] = tk.StringVar()
self.entryDict[pos] = tk.Entry(font = ('Arial', 20, 'bold'),
textvariable = self.inputDict[pos],
borderwidth = 1, width = 2,
justify = 'center',
validatecommand = vcmd,
validate = 'key')
And the code for self.VE is here:
def VE(self, P, pos):
if P in [str(i) for i in map(chr,range(49,58))]:
self.solution.insertVal(P,pos)
elif P == '': self.solution.removeVal(pos)
return P in [str(i) for i in map(chr,range(49,58))]+['']
My issue is that I cannot figure out how to get VE to take arguments that are not included in the list provided by this answer, duplicated below:
# valid percent substitutions (from the Tk entry man page)
# %d = Type of action (1=insert, 0=delete, -1 for others)
# %i = index of char string to be inserted/deleted, or -1
# %P = value of the entry if the edit is allowed
# %s = value of entry prior to editing
# %S = the text string being inserted or deleted, if any
# %v = the type of validation that is currently set
# %V = the type of validation that triggered the callback
# (key, focusin, focusout, forced)
# %W = the tk name of the widget
I believe I'll need to make my change to the line defining vcmd
, but I don't know what change to make to allow the validation command to take the position of the entry (that is, the value of pos
) as well as the attempted input. How do I add an argument to the validation command that isn't in that list?
Upvotes: 1
Views: 944
Reputation: 385880
The value you give to the validatecommand
is a tuple of the registered command and any arguments you want to pass to the command. Therefore, you can do something like this:
vcmd = self.register(self.VE)
for pos in ...:
self.entryDict[pos] = tk.Entry(...,
validatecommand=(vcmd, "%P", pos),
...)
...
def VE(self, P, pos):
print "P: %s pos: %s" % (P, pos)
Upvotes: 2