
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to open a new window by the user pressing a button in a tkinter GUI?
Tkinter creates a default window (i.e., master or root window) for every application. In tkinter, we can create a Popup window or a child window by defining a Toplevel(master) constructor. This will allow the tkinter application to create another window which can be resized dynamically by defining its size property.
Example
In this example, we have created a button widget that will open the new window with a text label.
#Import tkinter library from tkinter import * from tkinter import ttk #Create an instance of tkinter frame or window win= Tk() #Set the geometry of tkinter frame win.geometry("750x250") #Define a new function to open the window def open_win(): new= Toplevel(win) new.geometry("750x250") new.title("New Window") #Create a Label in New window Label(new, text="Hey, Howdy?", font=('Helvetica 17 bold')).pack(pady=30) #Create a label Label(win, text= "Click the below button to Open a New Window", font= ('Helvetica 17 bold')).pack(pady=30) #Create a button to open a New Window ttk.Button(win, text="Open", command=open_win).pack() win.mainloop()
Output
Running the above code will display a window that contains a button widget. When we click the button, it will open a new Window.
Now, click the "Open" button to open a new Window.
- Related Articles
- How to close a Tkinter window by pressing a Button?
- How to open link in a new window - JavaScript?
- How to specify where a Tkinter window should open?
- How do I open a website in a Tkinter window?
- Call the same function when clicking a Button and pressing Enter in Tkinter
- How do you create a Tkinter GUI stop button to break an infinite loop?
- How to open a new window on a browser using Selenium WebDriver for python?
- How to keep the window focus on the new Toplevel() window in Tkinter?
- How to add a margin to a tkinter window?
- How to build a simple GUI calculator using tkinter in Python
- Tkinter-How to get the current date to display in a tkinter window?
- How to bind a key to a button in Tkinter?
- Execute a script when a user is pressing a key in HTML?
- How to center a window on the screen in Tkinter?
- How to Disable / Enable a Button in TKinter?

Advertisements