(a) Consider the following program: from tkinter import from tkinter import mess
ID: 3830244 • Letter: #
Question
(a) Consider the following program: from tkinter import from tkinter import message box as tkmessageBox class GUI (object): def __init__self, parent): self.parent = parent self.parent.protocal(^*NM_DDELETE_WINDOW", self.quit_handler) self.initUI() def quit_handler(self): if tkmessageBox.askokcnce1("Quit", "Are you sure want to quit?"): self.parent.quit() def initUI(self): self.parent.title(^*Demo GUI^*) grid(row - 0, column = 0, sticky = MN) self.name_str = stringvar() self.my_name = Entry(self.parent, with = 30, textvariablesself.name_str) self.my_name.grid(row = 0, column = 1, sticky = MN, columnspan = 20) Button(self.parent, text ="say Hello", foreground="blue", command-self.display_messeges).grid(row=1, column=1, sticky=NM) Button(self.parent, text="Exit", foreground="red", command = self.display_message).grid(row=1, column=1, stick=NM) Button (self.parent, text="Exit", foreground="red", command=self.quit_handler).grid(row=1, column=2) def display_messages(self): tkmessageBox.showinfo("Hello", "Hello" "str(self.name_str.get())) def main(); root = Tk() muGUI = GUI(root) root.mainloop() if name " main": main() (a) Describe what each line of tte main function does. (b) Discuss the advantages of encapsulating the GUI functionality in a class. (c) Compare the functionally of the grid and pack geometry managers.Explanation / Answer
In the main() function,
root=Tk(),
where Tk() is used to initialize
the tcl/tk interpreter and creates the root
window, where other widgets are placed on this window.
The reference to this root window is assigned to
the variable "root".
Only one root window can be created. And it should
be created before other widgets creation.
myGUI=GUI(root),
in this statement, reference to the root window
is passed as argument to the GUI class constructor,
In this class, other widgets are created and
placed in the root window.
root.mainloop(),
window won't appear until we enter into the
mainloop.This loop is used to handle the user
interactions with the window,that is , it is used
to handle the events occurs in the window.
if the loop doesn't run, the events don't
get processed.
advantages of encapsulating the GUI functions in a class
a) OOP provides a modularization of code, each modular units are self-contained.
Modularization leads to the easy maintenance of code.
In the given program, in the constructor, we are calling the
initUI() function, to create other widgets and placed in the root
window.
initUI() function used to create one label, two buttons and placed in the root window.
two other functions,quit_handler(), display_message() to display the messages whenever
appropriate button is clicked.
Here, everything is compartmentalized,
a) creating the UI
b) displaying the message
Changes can be made easily in one unit, without affecting the other units.
b) reusability- rather than starting everything from scratch, we can use from something that already built!
Suppose we want to create another window same as this GUI class, we can this class in another class through
Inheritance.
Inheritance, reduces the development time of the application and makes the works easier.
c) compare the functionalites of pack and grid
grid manager is used to place the widgets in the row and column manner.
It is used for creating structures like tables.
pack manager is used to place all the widgets in either row or column.
It is used for creating structures like navigator bar.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.