WK4: Hands-on Event-Driven Programming with Tkinter

Welcome to Session 2
Hands-on Event-Driven Programming with Tkinter
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk

1 / 11
next
Slide 1: Slide
ProgrammingHigher Education (degree)

This lesson contains 11 slide, with text slide.

Items in this lesson

Welcome to Session 2
Hands-on Event-Driven Programming with Tkinter
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk

Slide 1 - Slide

Introduction to Tkinter
  • Tkinter: Python's standard GUI (Graphical User Interface) package
  • Event-Driven Programming recap
  • Programs respond to events (e.g., button clicks, key presses)
  • Main event loop listens for and handles events

Slide 2 - Slide

Basic Tkinter Widgets
Common widgets:
  1. Button
  2. Label
  3. Entry (text input)
  4. Checkbutton
  5. Radiobutton
Example:
Exercise: Create a window with a label and a button

Slide 3 - Slide

Handling Events in Tkinter
Event binding:
  • Connect a function to a widget event
Exercise: Modify the button to change the label text when clicked

Slide 4 - Slide

Layout Management
Tkinter geometry managers:
  1. pack() - simple
  2. grid() - table-like
  3. place() - precise positioning
Grid example:
Exercise: Create a login form with username and password fields

Slide 5 - Slide

 Discussion - Event Types
Common Tkinter events:

  • <Button-1>: Left mouse click
  • <Key>: Any key press
  • <Return>: Enter key press
  • <FocusIn>: Widget gains focus
  • <MouseWheel>: Mouse scroll
Question: Can you think of other useful events for our calculator?

Slide 6 - Slide

Advanced Event Handling
Multiple event bindings:
Exercise: Add keyboard support to your calculator (number keys and operators)

Slide 7 - Slide

Error Handling in GUI Applications
Importance of error handling in GUIs:
  • Prevent crashes
  • Provide user feedback
Example:  Implement error handling in your calculator

Slide 8 - Slide

Task: Extending the Calculator
Implement two ideas:
  1. Implement memory functions (M+, M-, MR, MC)
  2. Add square root and power functions
  3. Create a scientific calculator mode
Hands on Exercise
timer
1:00:00

Slide 9 - Slide

Recap
Key points:
  • Event-driven programming responds to user actions
  • Tkinter provides a simple way to create GUIs in Python
  • Proper event handling and error management are crucial

Slide 10 - Slide

Slide 11 - Slide