Graphical User Interface (GUI)
Using tkinter
Graphical User Interfaces (GUIs) are essential for making applications user-friendly. While
command-line programs work well for developers, normal users prefer clicking buttons,
typing into text boxes, and seeing visual results. If you are building a calculator, a
registration form, or a management system, you need to (i) create a window, (ii) add
elements like text and buttons, (iii) arrange them neatly, and (iv) make them actually do
something when clicked.
We will be learning it with four following examples.
1. Introduction to GUI (Creating a basic window)
2. Adding Labels and Buttons (Displaying text and clickable actions)
3. The Grid System (Arranging items in rows and columns)
4. Making it Interactive (Handling user input)
Comments are given in codes to explain what each term is doing. (If you copy code from
this PDF then after pasting into your Python editor, make sure it has proper indent
wherever required.)
INTRODUCTION TO GRAPHICAL USER INTERFACE (GUI)
from tkinter import * # Need to import this library which is standard for Python
GUI. # The asterisk (*) means we import everything from tkinter.
root = Tk() # This line creates the main window of your application. Think of
'root' as the foundation or the blank canvas where we will put everything else.
Not necessary to call it ‘root’ only, you can name it anything like ‘m’. But use
same name in entire code.
[Link]("My First App") # This sets the title that appears at the top of the
window.
[Link]("400x300") # This sets the initial size of the window.
# Format is "WidthxHeight".
# Everything we want to display must be written BEFORE the mainloop line.
[Link]() # This keeps the window open and listening for clicks or key
presses. # Without this, the window would open and close instantly.
After importing tkinter, we can create a function and put every code line which is written to
create GUI inside that function definition (by giving indent). Whenever we call our
function, our GUI will appear.
Why do this? It keeps things tidy. As your program gets bigger, having your GUI code
isolated in one place makes it much easier to find errors and make changes.
This is practiced in following code.
Prepared by Mishil Patel
ADDING LABELS AND BUTTONS
from tkinter import *
def GUI(): # We will define a function to create GUI.
root = Tk()
[Link]("400x300")
# A Label is just text displayed on the screen that the user cannot edit.
L1 = Label(root, text="Hello, World!")
# We pass 'root' to tell Python that this label belongs inside our window root
# You can give name of your choice to Label. Here I have given L1 which stands
for Label-1. Someone can name 'it my_label'. Use same name to everytime
interact with that Label. Same applies for Buttons also.
# A Button is clickable and can perform an action.
B1 = Button(root, text="Click Me!")
# Just creating widgets isn't enough; we must place them on the screen.
# Here we use pack(), which just stacks them one after another.
[Link]()
[Link]()
[Link]()
GUI() # Do not forget to call function, otherwise after executing code, you will
not see your GUI. You can give any name for defining function. Make sure to call
it by same name.
THE GRID SYSTEM
# pack() is simple, but for structured apps (like forms), we use grid().
# Imagine your window is a spreadsheet with rows and columns.
from tkinter import *
def GUI():
root = Tk()
# Creating widgets
L1 = Label(root, text="Name:")
L2 = Label(root, text="Password:")
# Placing them using grid
[Link](row=0, column=0) # Top-left corner
[Link](row=1, column=0) # Below the first label
# We can add padding (empty space) so it doesn't look too crowded.
# padx is space left/right, pady is space up/down.
[Link](row=0, column=0, padx=10, pady=10)
[Link]()
GUI()
# To organize even in better format, we can use ‘Frame’ inside grid.
Prepared by Mishil Patel
MAKING IT INTERACTIVE
# Now let's make a program that actually does something when a button is clicked.
# We will use an 'Entry' widget to get input from the user.
from tkinter import *
def GUI():
global E1, L1 # Declare E1 & L1 as global so that we can access them in other
functions.
root = Tk()
[Link]("400x300")
# 1. Create the Entry box (Input field)
E1 = Entry(root, width=30) # I named it E1 which stands for Entry-1. You can
give any name.
[Link](row=0, column=0, padx=10, pady=10)
# 2. Create the Button
# command=show_message - this line links the click of button to the function
named 'show_message' defined below.
# Note: Write function name WITHOUT parentheses '()'.
B1 = Button(root, text="Submit", command=show_message)
[Link](row=1, column=0, padx=10, pady=10)
# 3. Create a label to show the result
L1 = Label(root, text="")
[Link](row=2, column=0, padx=10, pady=10)
[Link]()
# This is the foundation of building forms. You take input using Entry,
# trigger logic using Button, and show output using Label.
# Function to run when button is clicked
def show_message():
user_text= [Link]() # .get() grabs whatever text is currently in the entry box
[Link](text="You typed: " + user_text)
# .config() allows us to change properties of a widget (like text) after
creating it.
# We are Keeping the GUI code separate from the button-click logic. This makes the
project easier to manage. When the code gets large, it becomes simpler to find
errors and make changes.
GUI() # Do not forget to call GUI() function.
We have only scratched the surface. tkinter has many more "widgets" (elements) that allow
you to build complex tools. You can add elements like, Checkboxes & Radio Buttons,
Message Boxes/Pop-ups (it will trigger pop-up windows to show warnings, errors, or
success messages), The Canvas (a drawing area to draw shapes - lines, circles, rectangles or
place images, to make graphs or simple games), Listbox & Scrollbars (for long list of
content) and Manu Bar (Ex- File, Edit, View, Help).
Prepared by Mishil Patel
By combining these elements, you can build full applications like Calculators, To-Do Lists /
Note Apps, Login Systems, Text Editors, Simple Games.
By combining GUI with Database, you can build Data Entry Tools like Inventory
Management Systems, Student Record Systems, or Library Management Tools.
Prepared by Mishil Patel