Python Tkinter Course for Beginners

Xasan GeeseyXasan Geesey
2 min read

Table of Contents

  1. Introduction to Tkinter

  2. Setting Up Your Environment

  3. Creating Your First Tkinter Window

  4. Tkinter Widgets

  5. Geometry Management

  6. Event Handling

  7. Building a Simple Application

  8. Practice Exercises


1. Introduction to Tkinter

Tkinter is Python's standard GUI (Graphical User Interface) package. It provides a fast and easy way to create GUI applications.

2. Setting Up Your Environment

Make sure you have Python installed. Tkinter is included by default in standard Python distributions.

Check Tkinter Installation

python -m tkinter

3. Creating Your First Tkinter Window

import tkinter as tk

root = tk.Tk()
root.title("My First Window")
root.geometry("300x200")
root.mainloop()

4. Tkinter Widgets

Here are some basic widgets you can use:

Label

label = tk.Label(root, text="Hello, Tkinter!")
label.pack()

Button

def on_click():
    print("Button clicked!")

button = tk.Button(root, text="Click Me", command=on_click)
button.pack()

Entry (Text Field)

entry = tk.Entry(root)
entry.pack()

Text (Multiline Text)

text = tk.Text(root, height=5, width=30)
text.pack()

5. Geometry Management

Tkinter provides several geometry managers:

Pack

Automatically packs widgets in a block.

widget.pack()

Grid

Uses a grid layout.

widget.grid(row=0, column=0)

Place

Places widgets at an absolute position.

widget.place(x=50, y=100)

6. Event Handling

Use command callbacks and event bindings to handle events.

def on_key(event):
    print("Key pressed:", event.char)

root.bind("<Key>", on_key)

7. Building a Simple Application

Here is a basic login form example:

import tkinter as tk

def login():
    username = entry_user.get()
    password = entry_pass.get()
    print(f"Username: {username}, Password: {password}")

root = tk.Tk()
root.title("Login Form")

# Username
tk.Label(root, text="Username").grid(row=0, column=0)
entry_user = tk.Entry(root)
entry_user.grid(row=0, column=1)

# Password
tk.Label(root, text="Password").grid(row=1, column=0)
entry_pass = tk.Entry(root, show="*")
entry_pass.grid(row=1, column=1)

# Login Button
tk.Button(root, text="Login", command=login).grid(row=2, column=1)

root.mainloop()

8. Practice Exercises

Practice 1: Create a Counter App

  • Create a window with a label and a button.

  • Each button click increases the counter by 1.

Practice 2: Build a Simple Calculator

  • Create buttons for digits and operations (+, -, *, /).

  • Display result on a label.

Practice 3: Todo List

  • Input field to add tasks.

  • Listbox to display tasks.

  • Button to remove selected task.


Happy coding!

0
Subscribe to my newsletter

Read articles from Xasan Geesey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Xasan Geesey
Xasan Geesey