Open a new Window with a button in Python-Tkinter

13 Jul, 2021
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 
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 modulefrom tkinter import *from tkinter.ttk import * # creates a Tk() objectmaster = Tk() # sets the geometry of main# root windowmaster.geometry("200x200")  # function to open a new window# on a button clickdef 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 clickbtn = Button(master,             text ="Click to open a new window",             command = openNewWindow)btn.pack(pady = 10) # mainloop, runs infinitelymainloop()
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 modulefrom 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() objectmaster = Tk() # sets the geometry of# main root windowmaster.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 clickbtn = 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 openedbtn.bind("<Button>",         lambda e: NewWindow(master)) btn.pack(pady = 10) # mainloop, runs infinitelymainloop()
Output:  
 
rajeev0719singh
Python-tkinter
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.