A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Event-Driven Programming

College Depth 85 in the knowledge graph ☐ I know this ☆ Set as goal
334prerequisites beneath it
See this on the map →
Defining and Calling FunctionsFunction Parameters and Argument Passing+2 more→
events event-handlers callbacks event-loop GUI paradigms

Core Idea

In event-driven programming, the order in which code runs is set by events, such as a mouse click, a key press, a timer firing or a network message, rather than by a fixed top-to-bottom sequence. The programmer writes small functions called event handlers (or callbacks) and registers each one for an event. An event loop, usually supplied by a library or the browser, waits for events and calls the matching handler each time one occurs. Graphical interfaces, web pages, games and many servers are built this way.

How It's Best Learned

Build a tiny interface with two buttons and a label, and have students predict what prints for different click orders. Contrast it with a script that runs once from top to bottom. Then deliberately put a slow loop inside a click handler so students see the whole window freeze, which makes the role of the event loop concrete.

Common Misconceptions

Explainer

The programs you have written so far run like a recipe. Line 1 runs, then line 2, and so on, with loops and conditionals changing the path. That style fits a program that reads input, computes and prints. It fits poorly when a program must wait for a person. A drawing app does not know whether the next action will be a click, a key press or a window resize.

Event-driven programming turns control around. You write a function for each thing that can happen, and you tell a library which function goes with which event. In a web page, `button.addEventListener("click", handleClick)` registers `handleClick` for clicks on that button. In Python's tkinter, `Button(root, text="Go", command=go)` does the same. These functions are called event handlers or callbacks, because the library calls them back later.

Notice that you pass `go`, not `go()`. A function name without parentheses is a value that refers to the function. Adding parentheses calls it right away. This is the most common beginner bug in event-driven code.

After registering handlers, the program starts an event loop, such as tkinter's `root.mainloop()`. The loop is simple in principle: wait for an event, look up its handler, call the handler, repeat. The operating system or browser collects events into a queue, and the loop takes them out one at a time.

Two consequences follow. First, you cannot predict the order in which handlers run. Code must work whether the user clicks "Load" before "Save" or the reverse, so handlers often check the program's state, for example disabling Save until a file is loaded. Second, in the common single-threaded design, only one handler runs at a time. While a handler is busy, no other events are handled and the window cannot even redraw. Good handlers do a small amount of work and return quickly. Long tasks are split into pieces with timers, or run in the background.

Events are not only for interfaces. Timers fire events at set intervals, which is how many games update their screens. Servers can react to incoming network messages in the same style. The underlying idea is always the same: the framework owns the main loop, and your code supplies the reactions.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Event-Driven Programming

Longest path: 86 steps · 334 total prerequisite topics

Prerequisites (4)

Leads To (0)

No topics depend on this one yet.