Python 3 Tkinter Script to Open Multiple New Windows or Tabs on Button Click Using Functions and Classes in GUI Desktop App Full Project For Beginners

Python 3 Tkinter Script to Open Multiple New Windows or Tabs on Button Click Using Functions and Classes in GUI Desktop App Full Project For Beginners

 

Welcome folks today in this blog post we will be opening multiple new windows or tabs on button click using functions and classes in tkinter gui desktop app in python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the below library using the pip command as shown below

 

pip install tkinter

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

 

First of all we will use the functions to do this task

 

# 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()

 

See also  Python 3 Tkinter Login Registration Form Page With MySQL Database Using mysql-connector Library GUI Desktop App Full Project For Beginners

 

Now we will be doing the same task using classes as shown below

 

# 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()

 

 

If you execute the above python script by typing the below command

 

python app.py

 

 

Leave a Reply