CodingNic

GUI Development

Building Apps with Tkinter

GUI Development 55 min read

Building Apps with Tkinter

Building Apps with Tkinter

You have learned windows, labels, buttons, entry widgets, layouts, and events.

Now it is time to combine those skills into a real application.

In this lesson, you will build a complete Tkinter project.


Project: Task Manager App

Build a desktop app where users can:

  • add tasks
  • view tasks
  • remove selected tasks
  • clear input
  • exit app

This project combines multiple GUI skills.


Final App Layout

text
---------------------------------
| Task Manager                  |
|                               |
| [ Entry: task name        ]   |
|                               |
| [Add] [Delete] [Clear] [Exit] |
|                               |
| - Buy milk                    |
| - Study Python                |
| - Call Mom                    |
---------------------------------

Skills You Will Use

  • Tkinter window
  • Label
  • Entry
  • Buttons
  • Listbox
  • pack()
  • Callbacks
  • Input handling

Step 1: Create Main Window

python
import tkinter as tk

window = tk.Tk()
window.title("Task Manager")
window.geometry("400x400")

window.mainloop()

Step 2: Add Title Label

python
title = tk.Label(
    window,
    text="Task Manager",
    font=("Arial", 18)
)
title.pack(pady=10)

Step 3: Add Entry Widget

python
task_entry = tk.Entry(window, width=30)
task_entry.pack(pady=5)

User types a task here.


Step 4: Add Listbox

A Listbox displays multiple items.

python
task_list = tk.Listbox(window, width=40, height=10)
task_list.pack(pady=10)

Step 5: Add Functions

Add Task

python
def add_task():
    task = task_entry.get()

    if task != "":
        task_list.insert(tk.END, task)
        task_entry.delete(0, tk.END)

Delete Selected Task

python
def delete_task():
    selected = task_list.curselection()

    if selected:
        task_list.delete(selected[0])

Clear Input

python
def clear_input():
    task_entry.delete(0, tk.END)

Exit App

python
def close_app():
    window.destroy()

Step 6: Add Buttons

python
tk.Button(window, text="Add", command=add_task).pack(pady=2)
tk.Button(window, text="Delete", command=delete_task).pack(pady=2)
tk.Button(window, text="Clear", command=clear_input).pack(pady=2)
tk.Button(window, text="Exit", command=close_app).pack(pady=2)

Full Working Code

python
import tkinter as tk

def add_task():
    task = task_entry.get()
    if task != "":
        task_list.insert(tk.END, task)
        task_entry.delete(0, tk.END)

def delete_task():
    selected = task_list.curselection()
    if selected:
        task_list.delete(selected[0])

def clear_input():
    task_entry.delete(0, tk.END)

def close_app():
    window.destroy()

window = tk.Tk()
window.title("Task Manager")
window.geometry("400x400")

title = tk.Label(window, text="Task Manager", font=("Arial", 18))
title.pack(pady=10)

task_entry = tk.Entry(window, width=30)
task_entry.pack(pady=5)

task_list = tk.Listbox(window, width=40, height=10)
task_list.pack(pady=10)

tk.Button(window, text="Add", command=add_task).pack(pady=2)
tk.Button(window, text="Delete", command=delete_task).pack(pady=2)
tk.Button(window, text="Clear", command=clear_input).pack(pady=2)
tk.Button(window, text="Exit", command=close_app).pack(pady=2)

window.mainloop()

Improvements You Can Add

  • Save tasks to file
  • Mark completed tasks
  • Double-click to edit
  • Better colors
  • Search tasks
  • Task count

Common Beginner Errors

Forgot mainloop()

Window will close.

Delete with No Selection

Check curselection() first.

Empty Task Added

Check input before inserting.


Code Along

Build the project exactly as shown.


Mini Challenge

Modify the app:

  1. Add button Clear All
  2. Show number of tasks in a label
  3. Press Enter key to add task

Real World Use Case

Task apps, note apps, inventory tools, and admin tools use the same GUI ideas.


Quiz

  1. What widget stores many items?
  2. What does curselection() return?
  3. How do you close the window?
  4. Why check empty input first?

Assignment

Build your own version of the Task Manager and add two custom features.


Summary

You built a complete Tkinter desktop app using widgets, layouts, events, and callbacks.