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
Here's my code, which was taken from here https://www.pythontutorial.net/tkinter/tkinter-toplevel/
import tkinter as tk
from tkinter import ttk
class Window(tk.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(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('300x200')
self.title('Main Window')
# place a button on the root window
ttk.Button(self,
text='Open a window',
command=self.open_window).pack(expand=True)
def open_window(self):
window = Window(self)
window.grab_set()
self.destroy()
if __name__ == "__main__":
app = App()
app.mainloop()
I added in self.destroy()
in the function def open_window(self)
. But it doesn't close the window created by this class: class App(tk.Tk)
.
You would need, under your design, to begin the mainloop of the second window somehow. Running into this problem before, what I found most helpful was to first start both windows and then withdraw them when they were not needed before and after they were shown and to run the .update()
method on both windows to keep them running simultaneously. Don't be dismayed, withdrawn windows does not mean a minimized windows and they are not easily accessible by the end user, but they are easily accessible to you with the .deiconify
method. As for destroying windows, I have found that in tkinter in python, that this was very, very finicky.
So, in your case:
import tkinter as tk
from tkinter import ttk
class Window(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.geometry('300x100')
self.title('Toplevel Window')
self.withdraw()
ttk.Button(self,
text='Close',
command=self.parent.destroy).pack(expand=True)
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('300x200')
self.title('Main Window')
def bar(self, window_unwithdraw_fn):
# place a button on the root window
ttk.Button(self,
text='Open a window',
command=window_unwithdraw_fn).pack(expand=True)
if __name__ == "__main__":
app = App()
window = Window(app)
def foobar():
window.deiconify()
window.grab_set()
app.withdraw()
app.bar(foobar)
while True:
app.update()
window.update()
Note a couple of things:
self.parent.destroy
implementation in Window
as it may have some unexpected consequences such as Widgets not behaving properlyApp
or Window
only withdraw them until the final close button is clickedWindow
, to deiconify it and withdraw the current Window, self
, in some new foo()
function