1

我有一组在 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 接受未包含在此答案提供的列表中的参数,重复如下:

# 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)以及尝试输入。如何向不在该列表中的验证命令添加参数?

4

1 回答 1

2

您给 的值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)
于 2013-08-14T17:21:04.730 回答