Open a new Window with a button in Python-Tkinter

Open a new Window with a button in Python-Tkinter

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.

1. Importing Tkinter:

To begin, you'll want to import the necessary module:

import tkinter as tk 

2. Function to Open a New Window:

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) 

3. Creating the Main Application Window:

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) 

4. Complete Example:

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.

Examples

  1. Python Tkinter create new window button example:

    • Description: Create a simple Tkinter window with a button that, when clicked, opens a new window.
    • Code 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() 
  2. Create popup window with button in Tkinter:

    • Description: Showcase creating a popup window with a button in Tkinter.
    • Code Example:
      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() 
  3. Python Tkinter switch between windows on button click:

    • Description: Explore switching between different windows in a Tkinter application based on button clicks.
    • Code Example:
      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() 
  4. Open a child window in Tkinter on button press:

    • Description: Demonstrate opening a child window in Tkinter when a button is pressed.
    • Code Example:
      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() 

More Tags

aspen self-updating shadow dart-async custom-taxonomy executable-jar drupal-7 postback ehcache react-async

More Programming Guides

Other Guides

More Programming Examples