Closing a window and opening a new one via a button in Tkinter

后端 未结 1 964
轮回少年
轮回少年 2021-01-14 04:15

I was wondering if there was a way to open a new instance of Toplevel() and close the current one via the press of a button, i.e. close the current window and open a new one

1条回答
  •  温柔的废话
    2021-01-14 04:54

    Yes, it's possible. To close an instance of Toplevel simply destroy it. You'll need to save a reference to the window. In your case I would either have Q1 destroy the window, or make a separate function that calls Q1 and then destroys the window. It all depends on what the main purpose of Q1 is.

    For example:

    def start(self):
        ...
        self.new_window = name_w
        ...
    
    def quit_window(self):
        self.Q1()
        self.new_window.destroy()
    

    If you have multiple of these you might need to store the window references in a list or dictionary, but the basic mechanism is the same: use .destroy() to destroy the window.

    This isn't the only way, of course. You could use lambda or functools.partial and a function that accepts the name of the window to destroy, or you could use nested functions, etc.

    0 讨论(0)
提交回复
热议问题