Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 40 ยท Projects and Practice

Tkinter Practice Exercises with Solutions and Project Ideas

Practice Tkinter with ten graded exercises: converters, timers, validation, tic-tac-toe, password meter and more, with tested logic solutions and project ideas.

You learn a GUI toolkit by building small programs, not by reading. This lesson is a set of exercises in increasing difficulty. Each has a short specification, a hint about the Tkinter parts to use, and a tested solution to the logic that you can run here. The window half is left to you; you can check it against the layout hints, and the logic half proves your window should show the right answers.

Tkinter cannot open windows in this page, so the code blocks for windows are read-only, and the runnable solutions are plain Python. A good way to work: write the logic function first, test it with a few asserts, then wire it to widgets.

How to use these exercises

  1. Read the spec and sketch the window on paper (which widgets, which layout manager).
  2. Write the logic as functions or a class without Tkinter.
  3. Write the window. Keep callbacks short: read inputs, call the logic, show the result.
  4. Handle bad input (empty fields, letters in a number box, Cancel in dialogs).
  5. Only then look at the solution and compare.

A good exercise workflow

Sketch Widgets and layout
Logic Functions, tested
Window Short callbacks
Errors Empty, wrong, Cancel

Build the smallest working version first.

1 / 2

Exercise 1: Temperature converter

Spec. Two entries, Celsius and Fahrenheit. Typing in one updates the other. Invalid text clears the other box instead of crashing.

Hints. Two StringVars with trace_add("write", ...). Beware of an endless loop: setting variable B inside A's trace triggers B's trace, which sets A. Guard with a flag.

Exercise 2: Countdown timer

Spec. A spinbox for minutes, Start/Pause/Reset buttons, and a big label MM:SS. At zero the window flashes and a message appears.

Hints. after(1000, tick), store the job id for after_cancel. Keep remaining seconds in an attribute, not in the label text.

Exercise 3: Tic-tac-toe

Spec. A 3 by 3 grid of buttons. Players alternate X and O, the game announces a winner or a draw, and "New game" resets it.

Hints. A Game class holds the board; buttons call game.play(index) and the view updates from game.board.

Exercise 4: Password strength meter

Spec. An entry with a show/hide toggle and a Progressbar that fills as the password gets stronger, with a text label (Weak, Fair, Good, Strong).

Hints. show="*" toggled by a checkbutton, validate="key" or a trace, a strength(password) function that returns 0 to 100.

Exercise 5: Stopwatch with laps

Spec. Start/Stop and Lap buttons, a running time with hundredths and a list of lap times.

Hints. Use time.perf_counter() differences, not counting ticks. Update with after(30, ...).

Exercise 6: Text editor

Spec. A Text widget with a scrollbar, a menu (New, Open, Save, Save As, Exit), Ctrl shortcuts, a status bar with line and column, and an unsaved changes check on close.

Hints. text.index("insert") gives "line.column"; text.edit_modified() tells whether the document changed (File Dialogs, Menus).

Exercise 7: Contact book

Spec. A Treeview of contacts, a form with name, phone and e-mail, buttons to add, update, delete, a search box and JSON saving.

Hints. Reuse the model pattern of the to-do project. Validate the e-mail with a simple rule and keep phone numbers as text.

Exercise 8: Bouncing balls

Spec. A canvas where clicking creates a ball with a random color and speed; balls bounce off the walls and a counter shows how many exist.

Hints. A Ball model class and one after loop for all balls (Canvas Animation with after()).

Exercise 9: Image viewer with zoom

Spec. Open a PNG or GIF, show it in a scrollable canvas, zoom with + and -, and show the file name in the title bar.

Hints. PhotoImage.zoom and subsample change size by integer factors; keep a reference to the image; set the scrollregion after each change (Canvas: Tags, Moving Items and Images).

Exercise 10: Quiz game with a settings dialog

Spec. Multiple-choice questions loaded from a JSON file, a score, a timer per question and a settings dialog (number of questions, time limit) that returns values.

Hints. A custom Toplevel dialog with a result (Toplevel Windows and Custom Dialogs), and Radiobuttons tied to one IntVar.

Solutions: the logic

Exercise 1: conversion with safe parsing

Python Run: temperature conversion that never crashes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 2: countdown formatting and ticking

Python Run: the countdown model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 3: tic-tac-toe engine

Python Run: tic-tac-toe game class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the window, button.config(text=game.board[i], state="disabled" if game.board[i] or game.winner else "normal") is all the view has to do after each move.

Exercise 4: password strength

Python Run: a strength score

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first value goes to bar["value"], the second to a label.

Exercise 5: stopwatch formatting

Python Run: time formatting and laps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Passing the clock in makes a stopwatch testable; the window passes time.perf_counter.

More project ideas

  • Unit converter with a Notebook (length, weight, temperature) driven by a table of factors.
  • Flashcards with a flip animation drawn on a canvas and spaced repetition.
  • Pomodoro timer with a system tray alternative (a small always-on-top window).
  • Expense tracker with SQLite, a Treeview and a bar chart drawn on a canvas.
  • Snake or Pong using the animation loop and key bindings.
  • File renamer: choose a folder, preview new names in a Treeview, apply with confirmation.
  • Markdown previewer: a Text widget with tags for headings and bold text.
  • Chat client that talks to a local server through a worker thread and a queue (Threads and Background Work).

Pick one, write the model first, then the window, and package it (Packaging with PyInstaller).

Common mistakes in exercises

  • Starting with the window and discovering later that the logic is tangled into the callbacks.
  • Handling only valid input.
  • Storing state in widget text (label["text"]) instead of in variables or a model.
  • Blocking the event loop in a timer or loop.
  • Copying a long solution without being able to explain every line; type it again from memory.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why guard with a flag when two Entry variables update each other through traces?

    An updating flag stops the echo update.
  2. 2. Why pass a clock function into Stopwatch?

    A fake clock makes the time-dependent logic deterministic and instant to test.
  3. 3. In the tic-tac-toe model, why does play() return False for a used cell?

    The model enforces the rules; the view only reacts to the result.
  4. 4. What is a good first step for any exercise?

    Logic first keeps the window thin and the program testable.

Summary

  • Practice with small, complete programs; model first, window second.
  • Inject clocks, dialogs and files so the logic can be tested.
  • Handle empty, wrong and cancelled input every time.
  • Use after for time, workers for slow jobs, and ids or models instead of widget text for state.
  • When you finish a project, package it and show it to someone.

Back to the track overview: Tkinter