有没有一种方法可以通过变量状态在for循环之外定义检查按钮单击功能?

时间:2019-06-27 20:54:54

标签: python tkinter

我正在尝试为所有显示在“选项”中的测试文件添加检查按钮。对于每个测试文件,我需要显示该文件下的所有测试功能。因此,我实现了以下代码:

def displayTestFiles(picks):
    #The below code populates the test files with check boxes  
    rowIndex1 = 3
    var1Index = 0
    for pick in picks:
        tempStore = IntVar() 
        var1.append(tempStore)
        chk = Checkbutton(root, text=pick, bg = "green", fg = "black", font ("Arial", 15), variable = var1[var1Index], command = displayTestFunctions).grid(row = rowIndex1, padx = 350, pady = 2, sticky= W) 
        rowIndex1 += 20
        var1Index += 1

但是我想更新另一个函数中的行号以分层显示。当我这样做时:command = displayTestFunctions(rowIndex1),它正在发送rowIndex1,即拾取长度。我不想发生。因此,我实现了以下代码,但是当我单击复选按钮时,它没有发生。因此,当我在下面的代码中调用displayTestFunctions(rowIndex1List [i])时,有没有办法放置命令函数?

#This function displays checkbuttons
def displayTestFiles(picks):
    #The below code populates the test files with check boxes  
    rowIndex1 = 3
    rowIndex1List = []
    var1Index = 0
    for pick in picks:
        tempStore = IntVar() 
        var1.append(tempStore)
        chk = Checkbutton(root, text=pick, bg = "green", fg = "black", font = ("Arial", 15), variable = var1[var1Index]).grid(row = rowIndex1, padx = 350, pady = 2, sticky= W) 
        rowIndex1List.append(rowIndex1)
        rowIndex1 += 20
        var1Index += 1

    for i in range(len(var1)):
        displayTestFunctions(rowIndex1List[i])

我想更新另一个函数“ displayTestFunctions”中的行号。

def displayTestFunctions():  
    for i in range(len(var1)):
        if var1[i].get() == 1:
            #The below code populates the test functions with check boxes of a particular test file selected
            with open("{}.py".format(tests[i][1]), "r") as fp:
                line = fp.readline()
                functionIndex = 0
                while line:
                    line = fp.readline()
                    if "def" in line:
                        if "test_" in line:
                            functionList.append([functionIndex, line.strip("# def:\n ( )")])
                            functionIndex += 1
                    else:
                        continue
            index2 = 4
            j = 0
            for funcList in functionList:
                stateStore = IntVar() 
                var2.append(stateStore)
                chk = Checkbutton(root, text = funcList, bg = "yellow", fg = "black", font = ("Arial", 15), variable = var2[j]).grid(row=index2, padx = 450, pady = 2, sticky= W) 
                index2 += 1
                j += 1

但是,当单击displayTestFiles函数中的复选按钮时,无法显示测试函数。因为,我不知道将检查按钮的命令放在for循环之外的位置。我正面临这个问题。请帮忙!

0 个答案:

没有答案