[Tutor] OOP help needed
Peter Otten
__peter__ at web.de
Wed Jul 27 04:12:22 EDT 2016
Jim Byrnes wrote:
> OOP has always driven me crazy. I read the material and follow the
> examples until I feel I understand them, but when I try to implement it
> I end up with an error filled mess.
>
> So I decided to give it another try. When I got to the chapter on
> tkinter I decided to solve all the exercises using OOP even though the
> book solutions did not use OOP. The first one went fine:
No, it didn't. The Goodbye.quit() method is missing the self argument and
uses the inexistent self.window attribute.
You don't see these bugs when you run the script because there is a global
quit()... let's say function... that is called instead of the method.
You can put a print() into Goodbye.quit() to verify the above.
> #exer1.py
>
> import tkinter
>
> class Goodbye:
> def __init__(self):
>
> self.frame = tkinter.Frame(window)
> self.frame.pack()
>
> self.goodbye_button = tkinter.Button(self.frame, text='Goodbye',
> #command=quit)
> command=lambda: quit() )
The lambda is superfluous -- command=quit will already invoke the global
quit(). But what you actually intended is achieved with command=self.quit.
self.quit is called "bound method".
> self.goodbye_button.pack()
>
> def quit():
print("you'll never see this")
> self.window.destroy()
>
> if __name__=='__main__':
> window = tkinter.Tk()
> myapp = Goodbye()
> window.mainloop()
More information about the Tutor
mailing list