0

I have a tkinter script, with an easy goal : propose 3 solutions in checkbutton, click "ok" after choice the option, and get this option. The code work great when I call it one time. But I need to do it in a loop on many possibilities, and here the probleme appear, after clicking the first "ok", I get the value, but the windows doesn't destroy completly and I cannot click on "ok" in the next option during the loop, and my program fail.

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

in the interface, choice2_action etc.. do the same as choice1 action for there button

when I try this :

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

it works

but when I try this:

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

it fail If you can help me to understand why, it can be greatful

Edit : I need to do in a loop because the goal of my script is a string correction. I have a list of correct word, and a list of wrong words. I do a loop among my wrong word, and difflib propose the 3 closest words of the correct list words. I want the user to choose manually among these 3 words, so I did this little interface, the user can check the correction or "none of them" if it is not satisfy that's why I iterate

kilag
  • 509
  • 1
  • 6
  • 19
  • You need to call `Tk()` *at most once* in your program - you can call `.withdraw()` on the window that gives you to hide it. Use `Toplevel()` instead for additional windows. The root problem is that all of your `IntVar`s are being created in the original Tk instance, and won't work in the subsequent instances you create. – jasonharper May 10 '19 at 13:33
  • Ok thank you, I will try something so for you, I can create fenetre=Tk() in my programme, and add fenetre in argument of main to avoid to recreate one at each loop? – kilag May 10 '19 at 14:43
  • @kilag: First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) means you **don't** have to `loop` the whole GUI. – stovfl May 13 '19 at 10:46

0 Answers0