[Tutor] OOP help needed
Jim Byrnes
jf_byrnes at comcast.net
Wed Jul 27 14:25:18 EDT 2016
On 07/27/2016 03:12 AM, Peter Otten wrote:
> 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.
OK right. I ended up concentrating on exer2 when the problem was in
exer1. I should have known better than using quit() as a name.
>> #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".
Ok, thanks.
>> 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()
>
Regards, Jim
More information about the Tutor
mailing list