Skip to content

Python! GUI Apps with Tkinter | Lectures 20

Lecture 20 Intro to GUI Apps with Tkinter

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.
Lecture 20 Intro to GUI Apps with Tkinter
Lecture 20 Intro to GUI Apps with Tkinter

πŸ§ͺ Try It Yourself!

Write a program that:

  1. Shows a label saying “Score: 0”
  2. 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 with tk.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.

Lectures 1 TO 20 Python Programming – BLOG PK

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.