Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Toplevel Widget in Tkinter

The Toplevel widget in tkinter is used to create child windows (or dialogs) separate from the main application window. These windows are useful for creating pop-up dialogs, preferences screens, secondary windows, and more.

Toplevel Widget in tkinter Tutorial:

1. Import Required Libraries:

import tkinter as tk

2. Initialize Main Application Window:

root = tk.Tk()
root.title("Toplevel Widget Tutorial")
root.geometry("400x300")

3. Creating a Basic Toplevel Window:

Here's how to create a secondary window using Toplevel.

def create_toplevel():
    top = tk.Toplevel(root)
    top.title("This is a Toplevel Window")
    top.geometry("300x200")

    label = tk.Label(top, text="Hello from the Toplevel Window!")
    label.pack(pady=20)
    
    close_btn = tk.Button(top, text="Close", command=top.destroy)
    close_btn.pack(pady=20)

button = tk.Button(root, text="Open Toplevel Window", command=create_toplevel)
button.pack(pady=40)

Notes:

  • Toplevel is very similar to the main window (Tk()), but it's intended to be used as a secondary window.
  • The destroy method can be used to close the Toplevel window.

4. Mainloop to Run the Application:

root.mainloop()

Complete Code:

import tkinter as tk

def create_toplevel():
    top = tk.Toplevel(root)
    top.title("This is a Toplevel Window")
    top.geometry("300x200")

    label = tk.Label(top, text="Hello from the Toplevel Window!")
    label.pack(pady=20)
    
    close_btn = tk.Button(top, text="Close", command=top.destroy)
    close_btn.pack(pady=20)

root = tk.Tk()
root.title("Toplevel Widget Tutorial")
root.geometry("400x300")

button = tk.Button(root, text="Open Toplevel Window", command=create_toplevel)
button.pack(pady=40)

root.mainloop()

In this tutorial, we explored the Toplevel widget in tkinter. The Toplevel window acts as a standalone window, but it's tied to the main application window in terms of event loops and closing behavior. This makes it an ideal choice for creating secondary windows in a tkinter application.

  1. Tkinter Toplevel window example:

    • Description: The Toplevel widget in Tkinter is used to create a new, independent window.
    • Code:
      import tkinter as tk
      
      def open_new_window():
          new_window = tk.Toplevel(root)
          new_window.title("New Window")
          new_window.geometry("300x200")
          tk.Label(new_window, text="This is a new window").pack()
      
      root = tk.Tk()
      
      # Create a button to open a new window
      open_button = tk.Button(root, text="Open New Window", command=open_new_window)
      open_button.pack(pady=20)
      
      root.mainloop()
      
  2. Modal and modeless Toplevel windows in Tkinter:

    • Description: A modal Toplevel window is a window that blocks interaction with other windows until it is closed. A modeless Toplevel window allows interaction with other windows while it is open.
    • Code:
      import tkinter as tk
      
      def open_modal_window():
          modal_window = tk.Toplevel(root)
          modal_window.title("Modal Window")
          modal_window.geometry("200x100")
          modal_window.transient(root)  # Make it modal
          tk.Label(modal_window, text="This is a modal window").pack()
          modal_window.grab_set()
          modal_window.wait_window()
      
      def open_modeless_window():
          modeless_window = tk.Toplevel(root)
          modeless_window.title("Modeless Window")
          modeless_window.geometry("200x100")
          tk.Label(modeless_window, text="This is a modeless window").pack()
      
      root = tk.Tk()
      
      # Create buttons to open modal and modeless windows
      modal_button = tk.Button(root, text="Open Modal Window", command=open_modal_window)
      modal_button.pack(pady=10)
      
      modeless_button = tk.Button(root, text="Open Modeless Window", command=open_modeless_window)
      modeless_button.pack(pady=10)
      
      root.mainloop()
      
  3. Closing Toplevel window in Tkinter:

    • Description: Close a Toplevel window using the destroy method.
    • Code:
      import tkinter as tk
      
      def open_and_close_window():
          new_window = tk.Toplevel(root)
          new_window.title("New Window")
          new_window.geometry("300x200")
          tk.Label(new_window, text="This window will close in 2 seconds").pack()
          root.after(2000, new_window.destroy)
      
      root = tk.Tk()
      
      # Create a button to open and close a new window
      open_close_button = tk.Button(root, text="Open and Close Window", command=open_and_close_window)
      open_close_button.pack(pady=20)
      
      root.mainloop()
      
  4. Tkinter Toplevel window title and icon:

    • Description: Set the title and icon for a Toplevel window using the title method and the iconbitmap method.
    • Code:
      import tkinter as tk
      
      def open_window_with_icon():
          new_window = tk.Toplevel(root)
          new_window.title("Window with Icon")
          new_window.iconbitmap("icon.ico")  # Replace "icon.ico" with the path to your icon file
          tk.Label(new_window, text="This is a window with an icon").pack()
      
      root = tk.Tk()
      
      # Create a button to open a window with an icon
      open_icon_button = tk.Button(root, text="Open Window with Icon", command=open_window_with_icon)
      open_icon_button.pack(pady=20)
      
      root.mainloop()