Home »
MCQs
GUI Programming with Python MCQs (Multiple-Choice Questions)
GUI (Graphical User Interface) programming in Python allows developers to create desktop applications with windows, buttons, menus, text fields, dialogs, images, and other interactive components. Python provides several GUI programming options, with Tkinter being the standard interface to the Tcl/Tk GUI toolkit.
GUI Programming with Python MCQs
These GUI Programming with Python MCQs cover Tkinter fundamentals, widgets, event handling, callbacks, geometry managers, dialogs, menus, Canvas, Tkinter variables, themed widgets, and practical GUI programming concepts.
List of GUI Programming with Python MCQs
Practice these multiple-choice questions to test your knowledge of GUI application development with Python and Tkinter.
1. What does GUI stand for in programming?
- General User Interface
- Graphical User Interface
- Graphical Utility Integration
- General Utility Interface
Answer: B) Graphical User Interface
Explanation:
GUI stands for Graphical User Interface. It allows users to interact with an application through visual elements such as windows, buttons, menus, and input fields.
2. Which Python package provides the standard interface to the Tcl/Tk GUI toolkit?
- tkinter
- pygui
- pythonui
- tkui
Answer: A) tkinter
Explanation:
tkinter is Python's standard interface to Tcl/Tk and provides classes for building graphical user interfaces.
3. Which statement correctly describes tkinter?
- It is a database management system
- It is a thin object-oriented layer over Tcl/Tk
- It is a Python web server
- It is a command-line compiler
Answer: B) It is a thin object-oriented layer over Tcl/Tk
Explanation:
The tkinter package provides Python classes and interfaces that wrap the Tcl/Tk GUI toolkit.
4. Which statement creates the main Tkinter application window?
root = tk.Tk()
root = tk.Window()
root = tk.MainWindow()
root = tk.Application()
Answer: A) root = tk.Tk()
Explanation:
Tk() creates the main application window and initializes the Tk interpreter associated with the application.
5. Which method starts Tkinter's event-processing loop?
root.start()
root.mainloop()
root.run()
root.execute()
Answer: B) root.mainloop()
Explanation:
mainloop() starts Tkinter's event loop, allowing the application to respond to user input, process events, and update the interface.
6. Why is the Tkinter event loop important?
- It compiles Python source code
- It continuously processes GUI events and updates the interface
- It creates database tables
- It installs Tkinter automatically
Answer: B) It continuously processes GUI events and updates the interface
Explanation:
A Tkinter interface depends on the event loop to process events, respond to user actions, and refresh the graphical interface.
7. What is a widget in Tkinter?
- A database record
- A graphical user-interface component
- A Python package manager
- A network connection
Answer: B) A graphical user-interface component
Explanation:
Widgets are GUI objects such as buttons, labels, entries, frames, text areas, checkbuttons, and listboxes.
8. Which Tkinter widget displays text that users normally do not edit directly?
- Entry
- Label
- Button
- Scale
Answer: B) Label
Explanation:
The Label widget displays text or an image and is commonly used for headings, descriptions, and status information.
9. Which Tkinter widget is commonly used for single-line text input?
- Text
- Entry
- Label
- Canvas
Answer: B) Entry
Explanation:
The Entry widget provides a single-line text input field.
10. Which Tkinter widget is designed for multi-line text?
- Entry
- Text
- Label
- Spinbox
Answer: B) Text
Explanation:
The Text widget is used for displaying and editing multiple lines of text.
11. Which option associates a Button with a callback function?
action
command
callback_function
onclick
Answer: B) command
Explanation:
The command option specifies the callback that Tkinter invokes when the button is activated.
12. Which is the correct way to assign a function named save_data as a button command?
command=save_data()
command=save_data
command="save_data()"
command=call(save_data)
Answer: B) command=save_data
Explanation:
The function object should be supplied without parentheses. Writing save_data() would call the function immediately while configuring the button.
13. Which method is used to arrange widgets using a two-dimensional row-and-column layout?
pack()
place()
grid()
locate()
Answer: C) grid()
Explanation:
The grid() geometry manager arranges widgets in rows and columns and is particularly useful for form-like interfaces.
14. Which geometry manager places widgets by stacking them against a side of their container?
grid()
pack()
place()
stack()
Answer: B) pack()
Explanation:
The pack() geometry manager arranges widgets along the top, bottom, left, or right side of their parent container.
15. Which geometry manager allows a widget to be positioned using coordinates such as x and y?
grid()
pack()
place()
coordinate()
Answer: C) place()
Explanation:
The place() geometry manager supports explicit positioning using absolute or relative coordinates.
16. What happens if a Tkinter widget is created but never given to a geometry manager?
- It is automatically placed at the center
- It normally does not appear in the interface
- It is automatically destroyed
- It becomes a menu item
Answer: B) It normally does not appear in the interface
Explanation:
Creating a widget does not automatically place it in the visible layout. It must be managed by a geometry manager such as grid(), pack(), or place().
17. Which statement about pack() and grid() is correct?
- They should always be mixed in the same parent
- They cannot be used anywhere in the same application
- They should not manage different widgets sharing the same parent
- They are identical geometry managers
Answer: C) They should not manage different widgets sharing the same parent
Explanation:
Tkinter documentation warns against using pack() and grid() to manage widgets that share the same parent. They can, however, be used in different nested containers.
18. Which option makes a packed widget expand to fill available horizontal space?
fill="x"
expand="x"
width="full"
stretch="x"
Answer: A) fill="x"
Explanation:
For the pack() manager, fill="x" allows the widget to expand horizontally within the available space.
19. Which grid() option controls where a widget is placed inside its allocated cell?
sticky
anchor_cell
position
align_cell
Answer: A) sticky
Explanation:
The sticky option controls how a widget is positioned or stretched within its grid cell using directions such as n, s, e, and w.
20. Which Tkinter method changes the title of the main window?
root.caption()
root.title()
root.heading()
root.name()
Answer: B) root.title()
Explanation:
The title() method sets the title displayed by the operating system's window manager for a top-level window.
21. Which method can set the initial size of a Tkinter window?
root.geometry("600x400")
root.size("600x400")
root.window_size(600,400)
root.dimension(600,400)
Answer: A) root.geometry("600x400")
Explanation:
The geometry() method accepts a geometry string such as "600x400" to specify the window's width and height.
22. Which Tkinter class creates a container commonly used to organize other widgets?
- Frame
- ContainerWidget
- GroupBox
- Panel
Answer: A) Frame
Explanation:
A Frame is a container widget that can hold other widgets and is often used to create separate layout regions.
23. Which widget is most appropriate for allowing a user to select one option from a set of mutually exclusive choices?
- Checkbutton
- Radiobutton
- Listbox
- Scale
Answer: B) Radiobutton
Explanation:
Radiobuttons are used when the user should select one option from a group of mutually exclusive choices.
24. Which Tkinter widget represents an independent on/off choice?
- Checkbutton
- Radiobutton
- Entry
- Listbox
Answer: A) Checkbutton
Explanation:
A Checkbutton provides a Boolean-style selection that can generally be independently enabled or disabled.
25. Which Tkinter variable class is commonly associated with a Boolean value?
- StringVar
- IntVar
- BooleanVar
- FloatVar
Answer: C) BooleanVar
Explanation:
BooleanVar is a Tkinter control variable that can be associated with widgets whose state is represented by a Boolean value.
26. Which method retrieves the current value stored in a Tkinter control variable?
read()
get()
value()
fetch()
Answer: B) get()
Explanation:
Control variables such as StringVar, IntVar, and BooleanVar provide get() to retrieve their current value.
27. Which method changes the value of a Tkinter control variable?
put()
assign()
set()
change()
Answer: C) set()
Explanation:
The set() method updates the value of Tkinter control variables such as StringVar and IntVar.
28. Which method binds a callback to a Tkinter event sequence?
event()
bind()
listen()
handle()
Answer: B) bind()
Explanation:
The bind() method associates an event sequence with a callback function.
29. What argument is normally passed to a callback registered with widget.bind()?
- A database connection
- An Event object
- A Tk variable
- A geometry manager
Answer: B) An Event object
Explanation:
A callback associated with bind() normally receives an event object containing information about the event, such as mouse coordinates or key details.
30. Which event sequence represents a left mouse-button click in Tkinter?
<MouseClick-1>
<Button-1>
<Click-Left>
<LeftMouse>
Answer: B) <Button-1>
Explanation:
In Tkinter event notation, <Button-1> represents a press of the first mouse button, normally the left mouse button.
31. Which event sequence is commonly used to detect keyboard input in Tkinter?
<Key>
<Keyboard>
<KeyPressEvent>
<Input>
Answer: A) <Key>
Explanation:
The <Key> event sequence can be used with bind() to respond to keyboard key-press events.
32. In a Tkinter event callback, which attribute commonly contains the pressed key's character?
event.key
event.char
event.character
event.input
Answer: B) event.char
Explanation:
For keyboard events, the Tkinter event object can provide the character associated with the key through the char attribute.
33. Which Tkinter widget provides a scrollable area for selecting items from a list?
- Listbox
- Label
- Entry
- CanvasButton
Answer: A) Listbox
Explanation:
The Listbox widget displays a list of items from which users can select one or more entries depending on its configuration.
34. Which Tkinter widget is designed for drawing shapes and other graphical objects?
- Canvas
- GraphicBox
- DrawingPanel
- PaintWidget
Answer: A) Canvas
Explanation:
The Canvas widget provides a drawing area where graphical objects such as lines, rectangles, ovals, text, and images can be created.
35. Which Canvas method creates a rectangle?
draw_rectangle()
create_rectangle()
rectangle()
add_rectangle()
Answer: B) create_rectangle()
Explanation:
The Canvas method create_rectangle() creates a rectangular graphical item using the supplied coordinates and configuration options.
36. Which Canvas method creates an oval or ellipse?
create_circle()
create_ellipse()
create_oval()
draw_oval()
Answer: C) create_oval()
Explanation:
Canvas uses create_oval() to create an oval or ellipse within a specified bounding box.
37. Which Tkinter module provides standard dialog boxes such as file selection dialogs?
tkinter.dialogs
tkinter.filedialog
tkinter.files
tkinter.windows
Answer: B) tkinter.filedialog
Explanation:
The tkinter.filedialog module provides native-style dialogs for selecting files and directories.
38. Which function opens a file-selection dialog for choosing an existing file?
filedialog.open()
filedialog.askopenfilename()
filedialog.selectfile()
filedialog.getfile()
Answer: B) filedialog.askopenfilename()
Explanation:
askopenfilename() displays an open-file dialog and returns the selected filename.
39. Which Tkinter module provides common message boxes?
tkinter.messagebox
tkinter.alerts
tkinter.messages
tkinter.notifications
Answer: A) tkinter.messagebox
Explanation:
The messagebox module provides functions for displaying standard message, warning, error, and confirmation dialogs.
40. Which function displays an informational message box?
messagebox.info()
messagebox.showinfo()
messagebox.information()
messagebox.alertinfo()
Answer: B) messagebox.showinfo()
Explanation:
showinfo() displays an informational message box with a title and message.
41. Which Tkinter widget provides a drop-down selection interface?
ttk.Combobox
ttk.DropDown
ttk.SelectBox
ttk.MenuBox
Answer: A) ttk.Combobox
Explanation:
ttk.Combobox is a themed Tk widget that combines a text-entry area with a list of selectable values.
42. What is the main purpose of the tkinter.ttk module?
- To provide themed Tk widgets
- To connect Python to SQL databases
- To create web applications
- To compile Tkinter applications
Answer: A) To provide themed Tk widgets
Explanation:
tkinter.ttk provides access to Tk's themed widget set, including widgets such as ttk.Button, ttk.Entry, ttk.Combobox, and ttk.Treeview.
43. Which ttk widget is particularly suitable for displaying hierarchical data?
ttk.Treeview
ttk.Hierarchy
ttk.TreeList
ttk.StructureView
Answer: A) ttk.Treeview
Explanation:
ttk.Treeview can display hierarchical items and can also be configured to display tabular columns.
44. Which Tkinter class creates an additional top-level window separate from the main root window?
tk.Toplevel
tk.SubWindow
tk.ChildWindow
tk.PopupWindow
Answer: A) tk.Toplevel
Explanation:
Toplevel creates another top-level window that can contain its own widgets and is managed by the window manager.
45. Which method schedules a callback to run after a specified number of milliseconds?
after()
delay()
schedule_after()
wait()
Answer: A) after()
Explanation:
The after() method schedules a callback to be called after a specified delay. It is commonly used for timers and periodic GUI tasks without blocking the event loop.
46. Why is a long-running task inside a Tkinter callback problematic?
- It automatically destroys all widgets
- It can block the event loop and make the GUI unresponsive
- It changes all widgets to ttk widgets
- It disables Python's garbage collector
Answer: B) It can block the event loop and make the GUI unresponsive
Explanation:
Tkinter processes GUI events through its event loop. A long-running callback can prevent the loop from processing other events, causing the interface to appear frozen.
47. Which widget is appropriate for allowing a user to select a value from a numerical range by moving a slider?
- Scale
- RangeBox
- SliderBox
- ValueSelector
Answer: A) Scale
Explanation:
The Tkinter Scale widget provides a slider-like control for selecting a value from a specified range.
48. Which Tkinter widget is commonly used to create a menu bar?
- Menu
- MenuBar
- MenuPanel
- CommandMenu
Answer: A) Menu
Explanation:
The Menu widget is used to construct menu systems, including application menu bars, submenus, and menu commands.
49. What does the winfo_toplevel() method provide for a Tkinter widget?
- The database associated with the widget
- The top-level window containing the widget
- The widget's parent Python module
- The current geometry manager
Answer: B) The top-level window containing the widget
Explanation:
winfo_toplevel() returns the top-level window that contains the widget. This is useful when code needs to access the window manager associated with the widget's containing window.
50. Consider the following code. What happens when the button is clicked?
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Ready")
label.pack()
def change_text():
label.config(text="Completed")
button = tk.Button(root, text="Run", command=change_text)
button.pack()
root.mainloop()
- The label text changes to "Completed"
- A new window named "Completed" is created
- The button is disabled permanently
- The program raises an error because callbacks cannot modify widgets
Answer: A) The label text changes to "Completed"
Explanation:
The button's command option is connected to change_text. When the button is activated, the callback calls label.config(), changing the displayed text.
Advertisement
Advertisement