Two windows template for tkinter
Open this design in the browser-based designer, rearrange it, and download the Python. Nothing to install, and you don't need an account to try it.
Open in designer →What this template shows you
- Opening a second window as a tk.Toplevel rather than a second tk.Tk()
- Letting the designer write the open_ handler for you when a project has more than one form
- Closing just the second window with self.root.destroy(), leaving the main one running
The Python it generates
This is the actual main.py you get when you download it: standard-library
tkinter, no framework, yours to edit:
import tkinter as tk
from tkinter import ttk
class MyApplication:
def __init__(self, root):
self.root = root
root.title("My application")
root.geometry("380x260")
self.label1 = tk.Label(root, text="Main window")
self.label1.place(x=30, y=30, width=300, height=28)
self.button1 = tk.Button(root, text="Open settings", command=self.open_settings)
self.button1.place(x=30, y=90, width=170, height=34)
self.label2 = tk.Label(root, text="The button above opens the second window.")
self.label2.place(x=30, y=150, width=320, height=60)
def open_settings(self):
return Settings(self.root)
class Settings:
def __init__(self, master):
root = tk.Toplevel(master)
self.root = root
root.title("Settings")
root.geometry("360x240")
self.checkbutton1_var = tk.IntVar()
self.checkbutton2_var = tk.IntVar()
self.label1 = tk.Label(root, text="Settings")
self.label1.place(x=30, y=25, width=280, height=28)
self.checkbutton1 = tk.Checkbutton(root, text="Dark mode", variable=self.checkbutton1_var)
self.checkbutton1.place(x=30, y=70, width=280, height=26)
self.checkbutton2 = tk.Checkbutton(root, text="Save automatically", variable=self.checkbutton2_var)
self.checkbutton2.place(x=30, y=105, width=280, height=26)
self.button1 = tk.Button(root, text="Close", command=self.on_button1)
self.button1.place(x=30, y=150, width=120, height=34)
self.label2 = tk.Label(root, text="")
self.label2.place(x=30, y=196, width=300, height=26)
def on_button1(self):
# self.root is this Toplevel, not the main window, so only
# this one closes and the application keeps running.
self.root.destroy()
if __name__ == "__main__":
root = tk.Tk()
app = MyApplication(root)
root.mainloop()
Save it and run it the usual way:
python main.py Make it your own. Edit the layout visually, then download the code.
Open the two windows template →Other templates
- Login form: Username + password entries with a working submit handler.
- Calculator: Display plus a 4×4 keypad with working press/clear/equals code.
- To-do list: Entry, listbox, and add/remove buttons, all wired up.
- Temperature converter: The classic beginner project: Celsius in, Fahrenheit out.