CodingNic

GUI Development

Events and Callbacks

GUI Development 42 min read

Events and Callbacks

Events and Callbacks

GUI apps are interactive.

Users click buttons, type text, move the mouse, and press keys.

These actions are called events.

Your program responds to events using callbacks.


What Is an Event?

An event is something the user does or something that happens in the GUI.

Examples:

  • button click
  • key press
  • mouse click
  • window close
  • text input

What Is a Callback?

A callback is a function that runs when an event happens.

Example:

  • User clicks button
  • Function runs

Button Click Example

python
import tkinter as tk

def hello():
    print("Hello")

window = tk.Tk()

button = tk.Button(window, text="Click", command=hello)
button.pack()

window.mainloop()

When the button is clicked, hello() runs.


Why No Parentheses?

Use:

python
command=hello

Not:

python
command=hello()

Parentheses run the function immediately.

We want Tkinter to run it later.


Update GUI on Event

python
import tkinter as tk

def change_text():
    label.config(text="Clicked!")

window = tk.Tk()

label = tk.Label(window, text="Ready")
label.pack()

button = tk.Button(window, text="Press", command=change_text)
button.pack()

window.mainloop()

Bind Other Events

Buttons use command, but many events use .bind().


Key Press Example

python
import tkinter as tk

def key_pressed(event):
    print("Key pressed")

window = tk.Tk()

window.bind("<Key>", key_pressed)

window.mainloop()

Mouse Click Example

python
def clicked(event):
    print("Mouse clicked")

window.bind("<Button-1>", clicked)

<Button-1> means left mouse click.


Why Event Parameter?

Functions used with .bind() receive an event object.

Example:

python
def clicked(event):

It contains useful info like:

  • mouse position
  • key pressed
  • widget source

Example: Show Mouse Position

python
def clicked(event):
    print(event.x, event.y)

Full Example

python
import tkinter as tk

def submit():
    text = entry.get()
    label.config(text="Hello " + text)

window = tk.Tk()

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

button = tk.Button(window, text="Submit", command=submit)
button.pack()

label = tk.Label(window, text="")
label.pack()

window.mainloop()

Common Beginner Errors

Using command=my_func()

Wrong for callbacks.

Forgetting Event Parameter in .bind()

Need:

python
def func(event):

No mainloop()

Events will not run.


Code Along

Create a button that changes background color when clicked.


Mini Challenge

Build an app:

  • Entry box
  • Label
  • Button = Show

When clicked, label shows typed text.

Also bind keyboard Enter key to do the same action.


Real World Use Case

Events and callbacks power forms, games, dashboards, shortcuts, menus, and interactive desktop apps.


Quiz

  1. What is an event?
  2. What is a callback?
  3. Why use command=function_name without parentheses?
  4. Why does .bind() often use event?

Assignment

Create a window where clicking anywhere shows the mouse position.


Summary

You learned how GUI apps respond to user actions using events and callback functions.