Tkinter widget keeps its state after being destroyed and re-built

276 views Asked by At

My code destroys some widgets then builds new widgets then in the end of the application a button asks if I want to get to the beginning. This button will call class constructor which re-initialize every variable and start re-drawing the same old widgets. The problem is that the widgets even if they are new keep their latest values before destruction.

def mapping(self):
        sort_frame = Frame(self.top_frame)
        sort_frame.grid(row=0,column=1)
        sort = False
        Checkbutton(sort_frame, text="Sort: ", variable=sort, onvalue=True, offvalue=False,command=lambda fr=sort_option_frame, nx =      next_button : self.enable_sort(fr,nx)).pack(side=TOP)
        next_button = Button(self.bottom_frame, text='Next',borderwidth=1, command=self.output_select)
        next_button.pack( side = RIGHT)

    def output_select(self):
        for widget in self.top_frame.winfo_children():
            widget.destroy()
        for widget in self.bottom_frame.winfo_children():
            widget.destroy()
        #new widgets drawing
        Button(self.bottom_frame, text='New file',borderwidth=1, command=self.restart).pack( side = TOP)

    #This UI resets the application for a new cycle
    def restart(self):
        for widget in self.top_frame.winfo_children():
            widget.destroy()
        for widget in self.bottom_frame.winfo_children():
            widget.destroy()
        self.__init__(self.root)

In this code for example, the Checkbutton on mapping will keep its latest values when mapping is called back in the new cycle.

I want the checkbutton to be new as if it's the first time it was created.

Thanks for your help

1

There are 1 answers

2
Bryan Oakley On BEST ANSWER

You can't use normal variables for the variable attribute. They need to be an instance of StringVar, IntVar, DoubleVar, or BooleanVar.