Let’s Start Python Programming For Beginners to Advanced. Intro to GUI Apps with Tkinter, Lectures 20
๐ Lecture 20: Intro to GUI Apps with Tkinter
๐ฏ Goal
By the end of this lecture, you will:
- Understand what a GUI (Graphical User Interface) is.
- Be able to create windows, buttons, and labels using
Tkinter
. - Know how to handle button clicks and update text on the screen.
- Build a simple calculator GUI or a click counter app.
๐ง What Is a GUI?
So far, weโve been writing programs that run in the command line, where everything is text-based.
A GUI (Graphical User Interface) lets users interact with buttons, windows, and images, just like real apps!
๐ฑ Think of your phone:
- You tap buttons
- You see icons and text
- You donโt type commands
Weโll use Pythonโs built-in module called tkinter
to make our own GUI apps.
๐ ๏ธ Setting Up โ Import Tkinter
Start by importing tkinter
:
import tkinter as tk
Then create a main window:
window = tk.Tk()
window.title("My First GUI App")
window.geometry("300x200") # Width x Height
To start the app, run:
window.mainloop()
This creates a basic empty window. Now letโs add things to it!
๐ณ Adding Labels
A label shows text on the screen.
label = tk.Label(window, text="Hello, GUI!")
label.pack() # Places the label in the window
You can change the font, color, and size, too:
label = tk.Label(window, text="Welcome to GUI World!", font=("Arial", 14), fg="blue")
label.pack()
๐ข Buttons โ Making Things Happen
A button does something when clicked.
Letโs define a function and connect it to a button:
def say_hello():
print("Button clicked!")
label.config(text="You clicked the button!")
button = tk.Button(window, text="Click Me", command=say_hello)
button.pack()
Now, when you click the button, it changes the label text!
๐ง Remember:
def
defines a function.command=
tells the button which function to run.

๐งช Try It Yourself!
Write a program that:
- Shows a label saying “Score: 0”
- Has a button that increases the score by 1 each time it’s clicked.
๐ก Sample Code:
score = 0
def increase_score():
global score
score += 1
score_label.config(text=f"Score: {score}")
window = tk.Tk()
window.title("Clicker Game")
window.geometry("300x200")
score_label = tk.Label(window, text="Score: 0", font=("Arial", 16))
score_label.pack()
button = tk.Button(window, text="Click to Increase Score", command=increase_score)
button.pack()
window.mainloop()
๐งฑ Building a Simple Calculator GUI
Letโs build a simple calculator with two buttons: one adds 1, one subtracts 1.
value = 0
def add_one():
global value
value += 1
update_label()
def subtract_one():
global value
value -= 1
update_label()
def update_label():
number_label.config(text=str(value))
window = tk.Tk()
window.title("Simple Calculator")
window.geometry("200x150")
number_label = tk.Label(window, text="0", font=("Arial", 24))
number_label.pack()
add_button = tk.Button(window, text="+1", command=add_one, width=10)
add_button.pack(pady=5)
subtract_button = tk.Button(window, text="-1", command=subtract_one, width=10)
subtract_button.pack(pady=5)
window.mainloop()
๐ This shows how to:
- Update a label dynamically
- Use spacing with
pady=5
- Create interactive buttons
๐ Challenge (Optional)
Make a color changer app that:
- Has a label showing the current color name.
- Two buttons: one for changing to red, another to blue.
- Changes background color and text accordingly.
Example:
def set_red():
color_label.config(text="Red", fg="red", bg="lightcoral")
def set_blue():
color_label.config(text="Blue", fg="blue", bg="lightblue")
window = tk.Tk()
window.title("Color Changer")
window.geometry("250x200")
color_label = tk.Label(window, text="Choose a color", font=("Arial", 18))
color_label.pack(pady=20)
red_button = tk.Button(window, text="Red", command=set_red, width=10)
red_button.pack(pady=5)
blue_button = tk.Button(window, text="Blue", command=set_blue, width=10)
blue_button.pack(pady=5)
window.mainloop()
๐ง Kids Corner
๐ง Imagine your robot friend now has a face โ with eyes, mouth, and buttons it can press!
๐ค Robot says:
Beep! I can see buttons now and click them with my mouse!
๐ Summary
- A GUI lets users interact with buttons, labels, and windows.
- Use
tkinter
to create GUI apps in Python. - Add labels with
tk.Label()
, buttons withtk.Button()
. - Connect functions to buttons using
command=
. - GUI programming makes your apps feel like real software!
Stay Updated
If you found this information useful, donโt forget to bookmark this page and Share and leave your feedback in the comment section below.