How do I open a new window in tkinter?
Use tk.Toplevel(). Creating a second tk.Tk() is the one thing to avoid: it starts a second, independent Tcl interpreter, and the two windows then misbehave in ways that are hard to trace.
import tkinter as tk
root = tk.Tk()
def open_settings():
win = tk.Toplevel(root)
win.title("Settings")
win.geometry("300x200")
tk.Label(win, text="Settings go here").pack(padx=20, pady=20)
tk.Button(root, text="Settings…", command=open_settings).pack()
root.mainloop() Keeping it in front, or making it modal
transient ties the new window to its parent so it stays above it and minimises with it. grab_set goes further and blocks input to every other window until this one closes — that is what makes a dialog modal.
win.transient(root)
win.grab_set() # modal: nothing else responds
win.wait_window() # and wait here until it closesStopping it opening twice
Clicking the button twice gives you two windows. Keep a reference and check it before opening another — winfo_exists() tells you whether the one you remember is still on screen.
def open_settings():
if getattr(self, "settings", None) and self.settings.winfo_exists():
self.settings.lift()
return
self.settings = tk.Toplevel(self.root)Getting data back out
A Toplevel is not a function, so it cannot return a value. Either give the second window a callback to hand its result to, or have it write to an attribute the caller reads once wait_window() returns. A class per window makes both patterns straightforward, which is why multi-window tkinter apps tend to end up organised that way.
Doing it without the lookup
Add a form and it becomes its own window class, with the main window getting an open_<form>() helper — so a login screen leading to a main screen is two tabs on the canvas rather than a wiring problem. The database layer, when on, is passed through to each window for you.
This example is ready to open: change it on the canvas and watch the Python update.
Open this example in the designer →