# main.py
import tkinter as tk
from tkinter import ttk
import random
class MainApplication:
def __init__(self, root):
self.root = root
self.root.title("Main Window")
self.root.geometry("400x300")
# Style for a simple, modern look
style = ttk.Style()
style.configure('Modern.TButton',
padding=10,
font=('Helvetica', 10))
self.header = ttk.Label(
root,
text="Toplevel Windows Demo",
font=('Helvetica', 16, 'bold'),
padding=20
)
self.header.pack()
self.create_buttons()
self.child_windows = []
self.center_window(self.root)
def create_buttons(self):
buttons_frame = ttk.Frame(self.root, padding="20")
buttons_frame.pack(fill=tk.BOTH, expand=True)
ttk.Button(
buttons_frame,
text="Open Basic Window",
style='Modern.TButton',
command=self.create_basic_window
).pack(pady=5, fill=tk.X)
ttk.Button(
buttons_frame,
text="Open Modal Window",
style='Modern.TButton',
command=self.create_modal_window
).pack(pady=5, fill=tk.X)
ttk.Button(
buttons_frame,
text="Open Positioned Window",
style='Modern.TButton',
command=self.create_positioned_window
).pack(pady=5, fill=tk.X)
ttk.Button(
buttons_frame,
text="Open Transient Window",
style='Modern.TButton',
command=self.create_transient_window
).pack(pady=5, fill=tk.X)
def create_basic_window(self):
window = tk.Toplevel(self.root)
window.title("Basic Window")
window.geometry("300x200")
ttk.Label(
window,
text="This is a basic Toplevel window",
padding=20
).pack()
ttk.Button(
window,
text="Close",
command=window.destroy
).pack(pady=10)
self.child_windows.append(window)
window.protocol("WM_DELETE_WINDOW",
lambda: self.cleanup_window(window))
def create_modal_window(self):
window = tk.Toplevel(self.root)
window.title("Modal Window")
window.geometry("300x200")
# Make it modal
window.grab_set()
window.transient(self.root)
ttk.Label(
window,
text="This window must be closed\nbefore using the main window",
padding=20
).pack()
ttk.Button(
window,
text="Close",
command=window.destroy
).pack(pady=10)
def create_positioned_window(self):
window = tk.Toplevel(self.root)
window.title("Positioned Window")
window.geometry("300x200")
# Random position on screen
x = random.randint(0, self.root.winfo_screenwidth() - 300)
y = random.randint(0, self.root.winfo_screenheight() - 200)
window.geometry(f"+{x}+{y}")
ttk.Label(
window,
text="This window appears at a random position",
padding=20
).pack()
ttk.Button(
window,
text="Close",
command=window.destroy
).pack(pady=10)
self.child_windows.append(window)
window.protocol("WM_DELETE_WINDOW",
lambda: self.cleanup_window(window))
def create_transient_window(self):
window = tk.Toplevel(self.root)
window.title("Transient Window")
window.geometry("300x200")
# Make it stay on top of the main window
window.transient(self.root)
ttk.Label(
window,
text="This window always stays\non top of the main window",
padding=20
).pack()
ttk.Button(
window,
text="Close",
command=window.destroy
).pack(pady=10)
self.child_windows.append(window)
window.protocol("WM_DELETE_WINDOW",
lambda: self.cleanup_window(window))
def cleanup_window(self, window):
if window in self.child_windows:
self.child_windows.remove(window)
window.destroy()
def center_window(self, window):
window.update_idletasks()
width = window.winfo_width()
height = window.winfo_height()
x = (window.winfo_screenwidth() // 2) - (width // 2)
y = (window.winfo_screenheight() // 2) - (height // 2)
window.geometry(f'+{x}+{y}')
if __name__ == "__main__":
root = tk.Tk()
app = MainApplication(root)
root.mainloop()