Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

Python tkinter close first window while opening second window

$
0
0

I'm trying to close the first window as the second window opens. Both windows close, or the first window closes and the second window never opens.

This question has a similar problem but was solved by addressing the imported libraries: Tkinter is opening a second windows when the first one is closing

This question also has a similar problem, but the solution keeps all windows active/open all the time, but withdraws the windows from view/user interaction when they are not being used. I want a solution without keeping 23 windows open at the same time. Python tkinter cloase first window while opening second window

I don't want to keep multiple windows open at the same time. What I am asking in this question is a MWE. So I've included only two windows in this question. My actual application has 23 windows in a tree-like structure.

quit and destroy both close the mainloop. Ideally, I'd use one mainloop and open and close windows as I need them rather than close one mainloop and sequentially open 22 other mainloops.

Here's my code, which was taken from here https://www.pythontutorial.net/tkinter/tkinter-toplevel/

from tkinter import *from tkinter import ttkclass Window(Toplevel):    def __init__(self, parent):        super().__init__(parent)        self.geometry('300x100')        self.title('Toplevel Window')        ttk.Button(self,                text='Close',                command=self.destroy).pack(expand=True)class App():    def __init__(self, root):        super().__init__()        self.root = root        root.geometry('300x200')        root.title('Main Window')        # place a button on the root window        ttk.Button(root,                text='Open a window',                command=self.open_window).pack(expand=True)    def open_window(self):        window = Window(self.root)        window.grab_set()        self.root.quit()if __name__ == "__main__":    root = Tk()    App(root)    root.mainloop()

I added in self.destroy() in the function def open_window(self). I also tried root.quit(). Both will close both windows.

I adapted this script so that class App takes root as an argument, rather than being a subclass of tk.Tk.


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>