简体   繁体   中英

__init__ method implemented on application called inside the main method

This code is part of the main calculator application mentioned here: https://pastebin.com/ECA2AQzY

I am new to python and from my understanding, "self" is the first argument being passed automatically for each instance of a class. Here, I am confused whether "app" would be self? and Frame is some superclass?

I do not understand why init is called on Frame within a main init method?

Also, why is lambda needed? Can it just not be command = self.appendToDisplay("7"))

What was the need of calling grid method on "app" object when the grid is called in the class itself?

calculator = Tk()
calculator.title("Calculator")
calculator.resizable(0, 0)

#================================================
class Application(Frame):
    def __init__(self, master, *args, **kwargs):
        Frame.__init__(self, master, *args, **kwargs)
        self.createWidgets()
    #================================================
    def createWidgets(self):
        self.sevenButton = Button(self, font=("Helvetica", 11), text="7", 
        borderwidth=0, command=lambda: self.appendToDisplay("7"))
        self.sevenButton.grid(row=1, column=0, sticky="NWNESWSE")

#================================================
app = Application(calculator).grid()
calculator.mainloop()

Let's try to answer your questions in order:

  1. Application intherrits from Frame , that is all methods implemented in Frame are also avaliable in Application . In addition Application can implement its own methods as well as override implementations in Frame . In the latter case, the implementations in Application replace the implementations in Frame . This is the case for the __init__() method. The method in Application replaces the one in Frame . Thus, the Frame() __init__() method is never called and that is typically not good, since there may be a lot of the functionallity in Frame() that needs explicit initialization. Therefore, Application.__init__() must explicitly call Frame.__init__() to get it to initialize.

  2. The difference between self.appendToDisplay("7") and lambda: self.appendToDisplay("7") is that in the first case the method appendToDisplay() is directly called and the result is returned. When using lambda instead of calling the function, a new function is defined. Thus, when using the lambda construct the result will not be directly available. Instead the result of the calculation will be delayed to a later time, when the command is to be executed. Thus, we want to display 7 on the display when the button is pressed and not now.

  3. The grid call in Application.createWidgets() refers to the button, to place that in the calculator Application . The Application.grid() call on the other hand, is placing the whole calculator itself. Thus, it is a hierachical thing, where the parts of the application are first placed and then the application itself.

I hope the above answers help you understand the concepts a bit better. It may be quite a bit to digest and you should try to find a few tutorials that explains the concepts in a bit more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM