我有一组在for循环中创建的输入框,并记录在我想要独立验证的字典中。创建它们的循环如下:
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')
self.VE的代码在这里:
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))]+['']
我的问题是我无法弄清楚如何让VE接受this answer提供的列表中未包含的参数,重复如下:
# 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
我相信我需要对定义vcmd
的行进行更改,但我不知道要允许验证命令获取条目的位置(即,值pos
)以及尝试输入。如何在不在该列表中的验证命令中添加参数?
答案 0 :(得分:1)
您赋予validatecommand
的值是已注册命令的元组以及要传递给命令的任何参数。因此,您可以这样做:
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)