The Wayback Machine - https://web.archive.org/web/20241001003840/https://www.geeksforgeeks.org/open-a-new-window-with-a-button-in-python-tkinter/
Open In App

Open a new Window with a button in Python-Tkinter

Last Updated : 13 Jul, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Python provides a variety of GUI (Graphic User Interface) such as PyQt, Tkinter, Kivy and soon. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to learn and implement as well. The word Tkinter comes from the tk interface. The tkinter module is available in Python standard library.
Note: For more information, refer to Python GUI – tkinter
 

Installation

For Ubuntu, you have to install tkinter module by writing following command:
 

sudo apt-get install python-tk 
 

When a Tkinter program runs, it runs a mainloop (an infinite loop) which is responsible for running a GUI program. At a time only one instance of mainloop can be active, so in order to open a new window we have to use a widget, Toplevel.
A Toplevel widget works pretty much like a Frame, but it opens in a separate top-level window, such windows have all the properties that a main window (root/master window) should have.
To open a new window with a button, we will use events.
Example 1: 
 

Python3




# This will import all the widgets
# and modules which are available in
# tkinter and ttk module
from tkinter import *
from tkinter.ttk import *
 
# creates a Tk() object
master = Tk()
 
# sets the geometry of main
# root window
master.geometry("200x200")
 
 
# function to open a new window
# on a button click
def openNewWindow():
     
    # Toplevel object which will
    # be treated as a new window
    newWindow = Toplevel(master)
 
    # sets the title of the
    # Toplevel widget
    newWindow.title("New Window")
 
    # sets the geometry of toplevel
    newWindow.geometry("200x200")
 
    # A Label widget to show in toplevel
    Label(newWindow,
          text ="This is a new window").pack()
 
 
label = Label(master,
              text ="This is the main window")
 
label.pack(pady = 10)
 
# a button widget which will open a
# new window on button click
btn = Button(master,
             text ="Click to open a new window",
             command = openNewWindow)
btn.pack(pady = 10)
 
# mainloop, runs infinitely
mainloop()


Output: 
 

Example 2: This will be a class based approach, in this we will create a class which will derive Toplevel widget class and will behave like a toplevel. This method will be useful when you want to add some other properties to an existing Toplevel widget class. 
Every object of this class will be a Toplevel widget. We will also use bind() method to register click event.
 

Python3




# This will import all the widgets
# and modules which are available in
# tkinter and ttk module
from tkinter import *
from tkinter.ttk import *
 
 
class NewWindow(Toplevel):
     
    def __init__(self, master = None):
         
        super().__init__(master = master)
        self.title("New Window")
        self.geometry("200x200")
        label = Label(self, text ="This is a new Window")
        label.pack()
 
 
# creates a Tk() object
master = Tk()
 
# sets the geometry of
# main root window
master.geometry("200x200")
 
label = Label(master, text ="This is the main window")
label.pack(side = TOP, pady = 10)
 
# a button widget which will
# open a new window on button click
btn = Button(master,
             text ="Click to open a new window")
 
# Following line will bind click event
# On any click left / right button
# of mouse a new window will be opened
btn.bind("<Button>",
         lambda e: NewWindow(master))
 
btn.pack(pady = 10)
 
# mainloop, runs infinitely
mainloop()


Output: 
 

 



Previous Article
Next Article

Similar Reads

How to open a website in a Tkinter window?
In this article, we are going to see how we can open a website in the Tkinter window. We can open a website in Tkinter using webview. This library allows us to view the HTML content in its GUI window. Syntax to install Tkinter and webview using the below commands. pip install tk pip install pywebviewMethod 1: Using webview.create_window() and webvi
2 min read
How to Close a Tkinter Window With a Button?
Prerequisites: Tkinter Python's Tkinter module offers the Button function to create a button in a Tkinter Window to execute any task once the button is clicked. The task can be assigned in the command parameter of Button() function. Given below are various methods by which this can be achieved. Method 1: Using destroy() Non-Class method Approach: I
3 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Python Tkinter - Frameless window
Prerequisite: Python GUI – tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. To create a Frameless window, we will use the overrideredirect() method. Syntax: root.over
1 min read
Hide and Unhide The Window in Tkinter - Python
Prerequisite: Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tki
2 min read
How to close only the TopLevel window in Python Tkinter?
In this article, we will be discussing how to close only the TopLevel window in Python Tkinter. Syntax: def exit_btn(): top.destroy() top.update() btn = Button(top,text='#Text of the exit button',command=exit_btn) Stepwise Implementation Step 1: First of all, import the library tkinter. from tkinter import * Step 2: Now, create a GUI app using tkin
3 min read
How to show webcam in TkInter Window - Python
Python offers various modules for creating GUI applications, out of which, Tkinter lets users do various tasks inside their app. Thus, while creating a GUI app, have you ever felt the need to let the user open the camera on a specific condition? Don't know, how to achieve that. Continue reading this article further to know about how to open the cam
5 min read
How To Get Rid Of Python Tkinter Root Window?
Python's Tkinter library offers a simple and effective way to create graphical user interfaces (GUIs) for your applications. However, when using Tkinter, you may encounter situations where you need to close or remove the root window, which is the main window of your GUI. Whether you're looking to clean up your interface or transition to another win
2 min read
How to open multiple files using "with open" in Python?
Python has various highly useful methods for working with file-like objects, like the with feature. But what if we need to open several files in this manner? You wouldn't exactly be "clean" if you had a number of nested open statements. In this article, we will demonstrate how to open multiple files in Python using the with open statement. What is
2 min read
How to open External Programs using Tkinter?
Prerequisite: Tkinter, os Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. In this article, w
2 min read
Transparent window in Tkinter
Prerequisite: Python GUI – tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. To create a transparent window, we will use the attributes() method. Syntax: root.attribut
1 min read
Dynamically Resize Buttons When Resizing a Window using Tkinter
Prerequisite: Python GUI – tkinter Button size is static, which means the size of a button cannot be changed once it is defined by the user. The problem here is while resizing the window size, it can affect the button size problem. So the solution here is, make a dynamic button, which means the button size will change as per window size. Let's unde
2 min read
How to Set a Tkinter Window With a Constant Size?
Prerequisite: Python GUI – tkinterminsizemaxsize The task here is to produce a Tkinter window with a constant size. A window with a constant size cannot be resized as per users' convenience, it holds its dimensions rigidly. A normal window on the other hand can be resized. Approach Import moduleDeclare Tkinter objectCreate a fixed sized window usin
2 min read
How to Create Full Screen Window in Tkinter?
Prerequisite: Tkinter There are two ways to create a full screen window in tkinter using standard python library for creating GUI applications. Method 1: Using attributes() function Syntax: window_name.attributes('-fullscreen',True) We will set the parameter '-fullscreen' of attributes() to True for setting size of our window to fullscreen and to F
3 min read
How to close a window in Tkinter?
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using tkinter is an easy task.
1 min read
Window Resizer Control Panel - Tkinter
Prerequisite: Tkinter Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. In this article, we wi
4 min read
How to add a margin to a tkinter window?
In this article, we will see how to add a margin to the Tkinter window. We will use the frame for adding the margin: Syntax: Frame(root, options) Approach: Importing the module.Create the main window (container)Use frame and frame.pack()Apply the event Trigger on the widgets. Without using frame method: C/C++ Code # importing the module from tkinte
2 min read
Make a Tkinter Window Jump to the Front
Making a Tkinter window jump to the front means bringing it to the top of all other windows, ensuring it grabs the user's attention. This can be particularly useful in applications that need to prompt the user for input or display important information. In this article, we will explore two different approaches to make a tkinter window jump to the f
2 min read
How To Center A Window On The Screen In Tkinter?
Tkinter provides a basic geometry manager to control the placement of widgets within a window, it doesn't offer a built-in method to automatically center a window on the screen. However, achieving this task is relatively straightforward with a few lines of code. In this article, we'll explore different approaches to centering a Tkinter window on th
2 min read
How To Change A Tkinter Window Background Color
Changing the background color of a Tkinter window is a common task for creating visually appealing GUI applications in Python. In this article, we will explore three different approaches to achieve this. Change a Tkinter Window Background ColorBelow are some of the ways by which we can change a Tkinter window background color using Python: Using th
2 min read
How To Bind The Enter Key To A Tkinter Window?
Tkinter is a Python library to develop GUI applications. Tkinter has many useful widgets that are necessary to build desktop applications. In this article, we will learn how to bind an Entry widget with a Tkinter Window. Binding Key in TkinterA key event occurs when the user clicks on any key on their keyboard. An action can be performed when any k
3 min read
Python | Add style to tkinter button
Tkinter is a Python standard library that is used to create GUI (Graphical User Interface) applications. It is one of the most commonly used packages of Python. Tkinter supports both traditional and modern graphics support with the help of Tk themed widgets. All the widgets that Tkinter also has available in tkinter.ttk.Adding style in a tkinter.tt
4 min read
Python | Add image on a Tkinter button
Tkinter is a Python module which is used to create GUI (Graphical User Interface) applications with the help of varieties of widgets and functions. Like any other GUI module it also supports images i.e you can use images in the application to make it more attractive. Image can be added with the help of PhotoImage() method. This is a Tkinter method
3 min read
How to create a pop up message when a button is pressed in Python - Tkinter?
In this article, we will see how to create a button and how to show a popup message when a button is pressed in Python. Tkinter is a standard Python Package for creating GUI applications. Tkinter may be a set of wrappers that implement the Tk widgets as Python classes. Python, when combined with Tkinter, provides a quick and straightforward way to
2 min read
Change color of button in Python - Tkinter
In this article, we are going to write a Python script to change the color of the button in Tkinter. It can be done with two methods: Using bg properties.Using activebackground properties.Prerequisite: Creating a button in tkinter, Python GUI – Tkinter Change the color of the button in Python - TkinterExample 1: using bg properties. If you want to
2 min read
Python Tkinter - Create Button Widget
The Tkinter Button widget is a graphical control element used in Python's Tkinter library to create clickable buttons in a graphical user interface (GUI). It provides a way for users to trigger actions or events when clicked. Note: For more reference, you can read our article: What is WidgetsPython Tkinter OverviewPython Tkinter TutorialTkinter But
6 min read
How to get a new API response in a Tkinter textbox?
In this article, we will look into how to make an API request and then insert it into a Tkinter textbox using Python, Before moving further we will cover some basic concepts related to our article that we will use frequently. Request: Requests are required to play with the web. If you want to use APIs, download entire Facebook pages, or do anything
4 min read
PyQt5 – How to open window in maximized format?
In this article we will see how to make a window to open in maximized format, which refer to full screen display window. We can do this by using showMaximized method which belongs to QWidget class. Syntax : self.showMaximized() Argument : It takes no argument. Action performed It will open the window in full screen format. Code : # importing librar
1 min read
How to switch to new window in Selenium for Python?
Selenium is the most used Python tool for testing and automation for the web. But the problem occurs when it comes to a scenario where users need to switch between windows in chrome. Hence, selenium got that cover as well. Selenium uses these methods for this task- window_handles is used for working with different windows. It stores the window ids
3 min read
PyQtGraph – Getting Window Flags of Plot Window
In this article we will see how we can get window flags of the plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) an
3 min read
Article Tags :
Practice Tags :