Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 14: GUI-Based Admission Form

Lesson 14 of 15 in the free Data Visualisation and Analytics Lab notes on Siksha Sarovar, written by Rohit Jangra.

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:

  1. 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 after mainloop() runs until the window closes. Control flow is inverted: the framework calls your code, not vice versa.
  2. Widgets and callbacks. tk.Label displays static text; tk.Entry is a single-line input read later with .get(); tk.Button(..., command=submit) registers the function submit as a callback. Crucially, command takes a function reference — writing command=submit() would call it immediately at construction time, the most classic tkinter bug.
  3. 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 via columnspan=2. padx/pady add breathing space. The rule: never mix grid and pack in 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:

FieldWidgetDemo value
Namename_entryDemo Student
Emailemail_entrydemo@sikshasarovar.com
Coursecourse_entryBCA

Procedure

  1. Inside run_gui_demo(), import tkinter, create the root window with tk.Tk() and set its title to "College Admission Form".
  2. Create three tk.Label widgets (Name, Email, Course) and grid them in column 0, rows 0–2, with 5 px padding.
  3. Create the matching tk.Entry widgets — name_entry, email_entry, course_entry — and grid them in column 1 beside their labels.
  4. Define submit(): read each entry with .get() into the captured dict, print it, and close the window with root.destroy().
  5. Create the Submit tk.Button with command=submit and grid it at row 3 with columnspan=2.
  6. Schedule autofill_and_submit via root.after(800, ...), then start root.mainloop(); the top-level try/except prints 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

  1. Writing command=submit() instead of command=submit — the parentheses invoke the function during widget construction, and the button then does nothing when clicked.
  2. Forgetting to call a geometry manager (grid/pack) on a widget — it is created but never appears, with no error to hint why.
  3. 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

  1. 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.
  2. What is a callback? A function passed by reference (e.g. command=submit) that the framework invokes later, when the associated event occurs.
  3. How does grid() position widgets? In a row/column table per container; columnspan lets a widget straddle multiple columns, as the Submit button does.
  4. How do you read what the user typed into an Entry? Call .get() on the Entry widget — typically inside the submit callback.
  5. What does root.after(800, fn) do? Schedules fn to run on the event loop after 800 milliseconds — tkinter's non-blocking alternative to time.sleep.
  6. Why does the code wrap the GUI in try/except? On headless systems tk.Tk() raises a TclError (no display); the except branch prints fallback data so the practical still produces output.