GUI with Class Question


#1

Hi, I copied this example from a book and have a question. So, in the last line, a object of type class ProcessButtonEvent is created. Now, when it enters the init method, does this obect have any data types? Are the variables window, btOK, and btCancel local variables? I’m a just really confused, isn’t init method only for creating giving attributes to a object. Why can you place “non initializing” code in the init method? Thanks.

from tkinter import *


class ProcessButtonEvent:
    def __init__(self):
        window = Tk()
        btOK = Button(window, text="OK", fg="red", command=self.processOK)
        btCancel = Button(window, text="Cancel", fg="blue", command=self.processCancel)

        btOK.pack()
        btCancel.pack()

        window.mainloop()


    def processOK(self):
        print("OK button was clicked.")

    def processCancel(self):
        print("Cancel button was clicked.")


ProcessButtonEvent()


#2

everything always has a data type, it must have. Python is dynamic, it handles data types for you

yes, they are.

says who? We get explicit control over init, we can place whatever we like in it. the only thing you need to understand is that init is called when you initialize (creating a class instance)