1. LPU MCA Semester 1
  2. CAB106
  3. Unit 6

CAB106 · Unit 6: Application development using OOP

Model-view concept and GUI using Tkinter

11 practice questions

Key points

  • Model holds data, view displays, controller connects them
  • Tk() is the main window and mainloop() starts the event loop
  • pack, grid and place are the geometry managers
  • Button command takes a function without brackets

The model-view idea separates what the program knows from how it is shown. The model holds data and rules, the view displays them, and a controller turns user actions (clicks, key presses) into model changes and view updates. Keeping them apart lets you change the interface without touching the logic, and a model can notify several views when it changes (the observer pattern).

Tkinter is Python's standard GUI toolkit. A program creates the root window with Tk(), adds widgets such as Label, Button, Entry (one line of input) and Frame (a container), arranges them with a geometry manager (pack, grid or place), and finally calls mainloop(), which starts the event loop that waits for user actions. A Button's command option takes a function without brackets; writing command=handler() would call it at once. StringVar, IntVar and DoubleVar hold values that linked widgets watch and update.

PieceRole
Tk()Main window
EntrySingle-line text input
gridRow and column layout
mainloop()Starts the event loop

Exam trap: pass command=handler, not command=handler(), and always end the program with mainloop() or the window closes immediately.

Try two questions

Question 1 · Tier 3, Apply

Which three geometry managers does Tkinter provide for laying out widgets?

  1. row, column and table
  2. pack, grid and place
  3. pack, align and stack
  4. grid, flow and border
Show the answer

B. Widgets are arranged with pack, grid or place.

Why not A: Those are layout terms, not Tkinter manager names.

Why not C: align and stack are not Tkinter managers.

Why not D: flow and border are layout managers of other toolkits.

Question 2 · Tier 3, Apply

In the model-view concept, which part holds the application's data and rules?

  1. The view
  2. The controller
  3. The event loop
  4. The model
Show the answer

D. The model owns the data and logic, the view shows it and the controller connects them.

Why not A: The view only displays the data.

Why not B: The controller passes user actions between the view and the model.

Why not C: The event loop waits for events and does not store application data.