Aim
To create a GUI-based college admission form using tkinter — Labels and Entry widgets arranged with the grid() geometry manager and a Submit Button wired to a callback — that captures Name, Email and Course on submission.
CO Mapping: CO1, CO2, CO3
Theory
tkinter is Python's standard GUI toolkit — a binding to Tcl/Tk that ships with the interpreter. Building a form exercises three ideas that separate GUI programs from the scripts written so far:
- Event-driven execution. A console script runs top to bottom and exits. A GUI instead constructs its widgets and then calls
root.mainloop()— the event loop — which blocks and waits, dispatching user events (clicks, keystrokes) to registered handlers. Nothing aftermainloop()runs until the window closes. Control flow is inverted: the framework calls your code, not vice versa. - Widgets and callbacks.
tk.Labeldisplays static text;tk.Entryis a single-line input read later with.get();tk.Button(..., command=submit)registers the functionsubmitas a callback. Crucially,commandtakes a function reference — writingcommand=submit()would call it immediately at construction time, the most classic tkinter bug. - Geometry management. Widgets appear only when handed to a geometry manager.
grid(row=r, column=c)places them in a conceptual table — here labels in column 0, entries in column 1, rows 0–2, and the button spanning both columns viacolumnspan=2.padx/padyadd breathing space. The rule: never mixgridandpackin the same container — tkinter raises an error as the two managers fight over layout.
The snippet adds one lab-specific device: root.after(800, autofill_and_submit) schedules a function on the event loop 800 ms after startup that types demo values into the entries and presses Submit programmatically — so the practical also completes on machines where nobody can interact. The surrounding try/except catches the TclError raised on headless environments (no display server) and prints fallback data instead.
Dataset
No input dataset applies — the form collects data. The auto-fill demo submits:
| Field | Widget | Demo value |
|---|---|---|
| Name | name_entry | Demo Student |
email_entry | demo@sikshasarovar.com | |
| Course | course_entry | BCA |
Procedure
- Inside
run_gui_demo(), import tkinter, create the root window withtk.Tk()and set its title to "College Admission Form". - Create three
tk.Labelwidgets (Name, Email, Course) and grid them in column 0, rows 0–2, with 5 px padding. - Create the matching
tk.Entrywidgets —name_entry,email_entry,course_entry— and grid them in column 1 beside their labels. - Define
submit(): read each entry with.get()into thecaptureddict, print it, and close the window withroot.destroy(). - Create the Submit
tk.Buttonwithcommand=submitand grid it at row 3 withcolumnspan=2. - Schedule
autofill_and_submitviaroot.after(800, ...), then startroot.mainloop(); the top-leveltry/exceptprints the same fallback data if no GUI is available.
Interpretation of Results
On a desktop, a small window appears with three label–entry rows and a Submit button; after 0.8 s the entries fill themselves and the console prints Submitted form data: {'name': 'Demo Student', 'email': 'demo@sikshasarovar.com', 'course': 'BCA'} before the window closes. The interesting behaviour is the order of events: mainloop() starts, idles, fires the after timer, the callback writes into the entries with .insert(0, ...), and submit() reads them back with .get() — demonstrating that Entry widgets are live objects whose state is read on demand, not variables captured at creation. On a headless online compiler, tk.Tk() raises immediately and the except branch prints the identical fallback dictionary — the program degrades gracefully rather than crashing, a pattern worth copying in any environment-dependent code.
Common Mistakes
- Writing
command=submit()instead ofcommand=submit— the parentheses invoke the function during widget construction, and the button then does nothing when clicked. - Forgetting to call a geometry manager (
grid/pack) on a widget — it is created but never appears, with no error to hint why. - Reading
entry.get()at construction time (outside the callback) — it returns an empty string because the user has not typed yet; entries must be read inside the event handler.
🎯 Viva Questions
- What does
root.mainloop()do? Starts the event loop that waits for and dispatches events; the program stays alive inside it until the window closes. - What is a callback? A function passed by reference (e.g.
command=submit) that the framework invokes later, when the associated event occurs. - How does
grid()position widgets? In a row/column table per container;columnspanlets a widget straddle multiple columns, as the Submit button does. - How do you read what the user typed into an Entry? Call
.get()on the Entry widget — typically inside the submit callback. - What does
root.after(800, fn)do? Schedulesfnto run on the event loop after 800 milliseconds — tkinter's non-blocking alternative totime.sleep. - Why does the code wrap the GUI in try/except? On headless systems
tk.Tk()raises aTclError(no display); the except branch prints fallback data so the practical still produces output.