Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Opening a new window (often called a secondary or child window) from the main window in a Tkinter application can be achieved using the Toplevel
widget. This tutorial will guide you through creating a new window when a button is clicked.
To begin, you'll want to import the necessary module:
import tkinter as tk
You'll want to define a function that gets called when the button is clicked. This function will create and show a new window:
def open_new_window(): # Create a new window new_window = tk.Toplevel(root) new_window.title("New Window") # Add a label to the new window label = tk.Label(new_window, text="This is the new window!") label.pack(pady=20, padx=20)
Initialize the main application window, set its title, and create a button to open the new window:
root = tk.Tk() root.title("Main Window") # Create a button to open the new window button = tk.Button(root, text="Open New Window", command=open_new_window) button.pack(pady=20, padx=20)
Combining the above steps, the full code to open a new window using a button in Tkinter is:
import tkinter as tk def open_new_window(): new_window = tk.Toplevel(root) new_window.title("New Window") label = tk.Label(new_window, text="This is the new window!") label.pack(pady=20, padx=20) root = tk.Tk() root.title("Main Window") button = tk.Button(root, text="Open New Window", command=open_new_window) button.pack(pady=20, padx=20) root.mainloop()
When you run this code, the main window will appear with a button labeled "Open New Window". Upon clicking the button, a new window will pop up displaying the text "This is the new window!".
You can further customize the new window by adding more widgets or functionalities as needed.
Python Tkinter create new window button example:
import tkinter as tk def open_new_window(): new_window = tk.Toplevel(root) new_window.title("New Window Example") label = tk.Label(new_window, text="This is a new window!") label.pack() root = tk.Tk() root.title("Main Window Example") button = tk.Button(root, text="Open New Window", command=open_new_window) button.pack() root.mainloop()
Create popup window with button in Tkinter:
import tkinter as tk from tkinter import messagebox def show_popup(): messagebox.showinfo("Popup Example", "This is a popup window!") root = tk.Tk() root.title("Popup Window Example") button = tk.Button(root, text="Show Popup", command=show_popup) button.pack() root.mainloop()
Python Tkinter switch between windows on button click:
import tkinter as tk def switch_windows(): if root.winfo_ismapped(): root.iconify() # Minimize the main window second_window.deiconify() # Bring the second window to focus root = tk.Tk() root.title("Main Window") button = tk.Button(root, text="Switch Windows", command=switch_windows) button.pack() second_window = tk.Toplevel(root) second_window.title("Second Window") second_window.geometry("300x200") second_window.withdraw() # Hide the second window initially root.mainloop()
Open a child window in Tkinter on button press:
import tkinter as tk def open_child_window(): child_window = tk.Toplevel(root) child_window.title("Child Window") label = tk.Label(child_window, text="This is a child window!") label.pack() root = tk.Tk() root.title("Main Window") button = tk.Button(root, text="Open Child Window", command=open_child_window) button.pack() root.mainloop()