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
volgende
Slide 1: Tekstslide
ProgrammingHigher Education (degree)

In deze les zit 11 slide, met tekstslide.

Onderdelen in deze les

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

Slide 1 - Tekstslide

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 - Tekstslide

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 - Tekstslide

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 - Tekstslide

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 - Tekstslide

 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 - Tekstslide

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

Slide 7 - Tekstslide

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 - Tekstslide

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 - Tekstslide

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 - Tekstslide

Slide 11 - Tekstslide