PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Medium
Question 37
Consider the following code snippet using Tkinter:
- from tkinter import Tk, Entry, StringVar
- def display_text():
- print(entry_text.get())
- root = Tk()
- entry_text = StringVar()
- entry_field = Entry(root, textvariable=entry_text)
- entry_field.pack()
- entry_text.set("Default text")
- root.mainloop()
Which of the following describes the behavior of the above script?
-
A
The script displays a window with an
Entrywidget containing the text "Default text", and when theEntrywidget is clicked, the functiondisplay_text()is called. -
B
The script does not display anything because it is missing a
Labelwidget. -
C
The script displays a window with an
Entrywidget containing the text "Default text", and when theEntrywidget text is changed, the functiondisplay_text()is called. -
D
The script displays a window with an
Entrywidget containing the text "Default text", but the functiondisplay_text()is not called by any event.
Reveal correct answer
Correct answer: D
A.
Although the script displays an Entry widget with "Default text", there is no event binding that would call the display_text() function when the Entry widget is clicked.
B.
The script does display a window with an Entry widget. A Label widget is not required for the window to display something.
C.
While the Entry widget is displayed with "Default text", the function display_text() is not called when the Entry widget text is changed. There is no event binding that would trigger the function in this case.
D.
The script does display a window with an Entry widget containing the text "Default text". However, although the display_text() function would print the content of the Entry widget when called, there is no event in the script that calls this function.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
