循环破坏tkinter接口吗?

时间:2019-05-10 13:25:24

标签: python loops tkinter destroy

我有一个tkinter脚本,目标很简单: 在复选按钮中提出3个解决方案,选择选项后单击“确定”,并获得此选项。 一次调用该代码,效果很好。 但是我需要在许多可能性上循环进行操作,在这里出现问题,单击第一个“确定”后,我得到了值,但窗户并没有完全破坏,因此我无法在窗口中单击“确定”循环中的下一个选项,我的程序就会失败。

class Interface(Frame):

    def __init__(self, fenetre, name, choice, **kwargs):
        Frame.__init__(self, fenetre, width=768, height=576, **kwargs)
        self.choice = choice
        self.index = np.nan
        self.pack(fill=BOTH)
        self.message = Label(self, text="Current Name : " + name)
        self.message.pack()

        self.var_case1 = IntVar()
        self.choice1 = Checkbutton(fenetre, text=choice[0], variable=self.var_case1, command=self.choice1_action)
        self.choice1.pack(side="left", anchor=W)
        self.var_case2 = IntVar()
        self.choice2 = Checkbutton(fenetre, text=choice[1], variable=self.var_case2, command=self.choice2_action)
        self.choice2.pack(side="left", anchor=W)
        self.var_case3 = IntVar()
        self.choice3 = Checkbutton(fenetre, text=choice[2], variable=self.var_case3, command=self.choice3_action)
        self.choice3.pack(side="left", anchor=W)
        self.var_case4 = IntVar()
        self.choice4 = Checkbutton(fenetre, text="None of solution", variable=self.var_case4, command=self.choice4_action)
        self.choice4.pack(side="left", anchor=W)

        self.bouton_quitter = Button(self, text="Ok", command=self.ok)
        self.bouton_quitter.pack(side="right", anchor=W)

    def ok(self):
        if self.var_case1.get() == 1:
            self.index = (self.choice[0])
            self.quit()
        if self.var_case2.get() == 1:
            self.index = (self.choice[1])
            self.quit()
        if self.var_case3.get() == 1:
            self.index = (self.choice[2])
            self.quit()
        if self.var_case4.get() == 1:
            self.quit()

    def choice1_action(self):

        self.choice2.deselect()
        self.choice3.deselect()
        self.choice4.deselect()


def main(name, list_name):
    fenetre = Tk()
    interface = Interface(fenetre, name, list_name)
    interface.mainloop()
    i = interface.index
    interface.destroy()
    return i

在界面中,choice2_action等。与那里按钮的choice1操作相同

当我尝试这个时:

value = main("Avenger", ['Thor', 'Hulk', 'Captain'])
if value != np.nan:
    print(value)

有效

但是当我尝试这样做时:

for i in range(5):
    value = main("Avenger", ['Thor', 'Hulk', 'Captain'])
    if value != np.nan:
        print(value)

失败 如果您能帮助我理解原因,那就太好了

编辑: 我需要循环执行,因为脚本的目标是字符串校正。 我有一个正确的单词列表和一个错误的单词列表。 我在错误的单词之间循环,然后difflib提出正确列表单词中最接近的3个单词。我希望用户在这3个单词中进行手动选择,所以我做了这个小界面,用户可以检查更正,如果不满意则可以“不检查” 这就是为什么我迭代

0 个答案:

没有答案