Python turtle and tkinter

Click Counter

Build a real window with a button and a counter, and see how fast you can click.

Day 5 45 to 60 minutes 7 steps Key Stage 3
Start step 1

So far the turtle has drawn pictures. Today you will make an app with a window, a big number and a button. Every click adds one, and the number turns green when you reach 10. The windows, labels and buttons come from tkinter, which is built into Python.

You will learn

  • Open a window with tkinter
  • Show text with a Label and add a Button
  • Run a function when a button is clicked
  • Change what a label says while the program runs

You will need

  • A computer with Thonny or IDLE, or this page's Run it here button
  • Day 3's project, for functions

Words to know

tkinter
Python's built in toolkit for windows, labels, buttons and boxes to type in.
widget
A part of a window: a label, a button or a box to type in.
event
Something that happens while the program runs, such as a click.
global
Lets a function change a variable made outside it.

Step by step

  1. Step 1: An empty window

    Save a new file as counter.py and type this. tk.Tk() makes a window, title and geometry set its title and its size in pixels, 300 wide and 250 high, and mainloop() keeps it open, waiting for clicks.

    counter.pyPython
    import tkinter as tk window = tk.Tk()window.title("Click Counter")window.geometry("300x250") window.mainloop()
    An empty window called Click Counter.
    Watch out Call your file counter.py, never tkinter.py.
  2. Step 2: Show a number

    A Label shows text in the window. font=("Arial", 48, "bold") makes it big. Nothing appears until you pack() it, which places it in the window, under anything packed before it.

    New: lines 7 to 9.

    counter.pyPython
    import tkinter as tk window = tk.Tk()window.title("Click Counter")window.geometry("300x250") label = tk.Label(window, text="0", font=("Arial", 48, "bold"))label.pack() window.mainloop()
    A big 0 at the top of the window.
  3. Step 3: Add a button

    A Button works the same way. Run it and click the button: nothing happens yet, because it has no job to do.

    New: lines 10 to 12.

    counter.pyPython
    import tkinter as tk window = tk.Tk()window.title("Click Counter")window.geometry("300x250") label = tk.Label(window, text="0", font=("Arial", 48, "bold"))label.pack() button = tk.Button(window, text="Click me!", font=("Arial", 14))button.pack(pady=5) window.mainloop()
    A button under the number. Clicking it does nothing yet.
    Tip pady=5 adds 5 pixels of space above and below the button.
  4. Step 4: Make it count

    The function add_one adds 1 to count and puts the new number on the label. label.config(text=...) changes what a label says, and str turns the number into text. command=add_one tells the button to call the function when it is clicked: no brackets after add_one, because the button calls it later, not now.

    global count lets the function change the count made outside it.

    New: lines 2 to 8 and 17.

    counter.pyPython
    import tkinter as tk count = 0 def add_one():    global count    count = count + 1    label.config(text=str(count)) window = tk.Tk()window.title("Click Counter")window.geometry("300x250") label = tk.Label(window, text="0", font=("Arial", 48, "bold"))label.pack() button = tk.Button(window, text="Click me!", font=("Arial", 14), command=add_one)button.pack(pady=5) window.mainloop()
    After three clicks, the label says 3.
    Watch out Write command=add_one, not command=add_one(). With brackets, the function runs once when the window opens, and never again.
  5. Step 5: Add a title

    Things appear in the order you pack them, so to put a title at the top, make and pack it before the number.

    New: lines 14 to 16.

    counter.pyPython
    import tkinter as tk count = 0 def add_one():    global count    count = count + 1    label.config(text=str(count)) window = tk.Tk()window.title("Click Counter")window.geometry("300x250") title = tk.Label(window, text="How fast can you click?", font=("Arial", 14))title.pack(pady=10) label = tk.Label(window, text="0", font=("Arial", 48, "bold"))label.pack() button = tk.Button(window, text="Click me!", font=("Arial", 14), command=add_one)button.pack(pady=5) window.mainloop()
    A title over the counter.
  6. Step 6: Start again

    A second button resets the count. Its function sets count back to 0, and the label back to "0".

    New: lines 9 to 13 and 28 to 30.

    counter.pyPython
    import tkinter as tk count = 0 def add_one():    global count    count = count + 1    label.config(text=str(count)) def reset():    global count    count = 0    label.config(text="0") window = tk.Tk()window.title("Click Counter")window.geometry("300x250") title = tk.Label(window, text="How fast can you click?", font=("Arial", 14))title.pack(pady=10) label = tk.Label(window, text="0", font=("Arial", 48, "bold"))label.pack() button = tk.Button(window, text="Click me!", font=("Arial", 14), command=add_one)button.pack(pady=5) reset_button = tk.Button(window, text="Reset", command=reset)reset_button.pack() window.mainloop()
    A Reset button under Click me!
  7. Step 7: Turn green at 10

    if count >= 10 means if the count is 10 or more. Then fg="green" turns the number's text green. fg stands for foreground, the colour of the text. Reset must turn it black again.

    New: lines 9, 10 and 15.

    counter.pyPython
    import tkinter as tk count = 0 def add_one():    global count    count = count + 1    label.config(text=str(count))    if count >= 10:        label.config(fg="green") def reset():    global count    count = 0    label.config(text="0", fg="black") window = tk.Tk()window.title("Click Counter")window.geometry("300x250") title = tk.Label(window, text="How fast can you click?", font=("Arial", 14))title.pack(pady=10) label = tk.Label(window, text="0", font=("Arial", 48, "bold"))label.pack() button = tk.Button(window, text="Click me!", font=("Arial", 14), command=add_one)button.pack(pady=5) reset_button = tk.Button(window, text="Reset", command=reset)reset_button.pack() window.mainloop()
    After 12 clicks the number is green.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Count down

    Add a third button that takes 1 away.

    Show a hint

    Copy add_one and call the copy take_one. Change count + 1 to count - 1, and make a button with command=take_one.

  2. Super clicker

    Add a +10 button for when you are in a hurry.

    Show a hint

    One more function, one more button. The function adds 10 instead of 1.

  3. Ten second challenge

    Count down from 10 seconds, then switch off the Click me! button so the score cannot change.

    Show a hint

    window.after(1000, tick) calls the function tick in 1000 milliseconds, one second. Inside tick, take 1 off the time and call window.after(1000, tick) again, until the time is up. Then button.config(state="disabled") switches the button off.

    Show one answer
    counter.pyPython
    import tkinter as tk count = 0 def add_one():    global count    count = count + 1    label.config(text=str(count)) time_left = 10 def tick():    global time_left    time_left = time_left - 1    timer.config(text=str(time_left) + " seconds left")    if time_left > 0:        window.after(1000, tick)    else:        button.config(state="disabled") window = tk.Tk()window.title("Click Counter")window.geometry("300x250") timer = tk.Label(window, text="10 seconds left", font=("Arial", 14))timer.pack(pady=10) label = tk.Label(window, text="0", font=("Arial", 48, "bold"))label.pack() button = tk.Button(window, text="Click me!", font=("Arial", 14), command=add_one)button.pack(pady=5) window.after(1000, tick)window.mainloop()

The finished code

Show the whole program
counter.pyPython
import tkinter as tk count = 0 def add_one():    global count    count = count + 1    label.config(text=str(count))    if count >= 10:        label.config(fg="green") def reset():    global count    count = 0    label.config(text="0", fg="black") window = tk.Tk()window.title("Click Counter")window.geometry("300x250") title = tk.Label(window, text="How fast can you click?", font=("Arial", 14))title.pack(pady=10) label = tk.Label(window, text="0", font=("Arial", 48, "bold"))label.pack() button = tk.Button(window, text="Click me!", font=("Arial", 14), command=add_one)button.pack(pady=5) reset_button = tk.Button(window, text="Reset", command=reset)reset_button.pack() window.mainloop()
Notes for the teacher

Timing

  1. 10 minutes: steps 1 to 3. The window appears quickly, which students enjoy.
  2. 20 minutes: step 4 is the big idea, a button calling a function. Draw it on the board: click, then add_one runs, then the label changes.
  3. 15 minutes: steps 5 to 7 and the tests.

Mistakes to look out for

  1. command=add_one() with brackets: the count goes to 1 as the window opens and the button then does nothing.
  2. No global count in the function: an UnboundLocalError about count, the first time the button is clicked.
  3. label.config(text=count) without str() works in tkinter, but label.config(text="Score: " + count) crashes with TypeError: can only concatenate str (not "int") to str.
  4. Forgetting pack(): the widget is made but never appears.
  5. Code after window.mainloop() does not run until the window is closed.

Going further

This is event driven programming: the program sets things up, then waits for events (clicks) and runs a function for each one. Compare it with the turtle programs, which ran from top to bottom and stopped.