python - Calling a method with an event handler -
why, if put "normal" call method button1click()
in bind
call, program not start? removing parenthesis problem solved. i'm using program reference: thinking in tkinter
also, why should add event
argument button1click()
method?
from tkinter import * class myapp: def __init__(self, parent): self.myparent = parent self.mycontainer1 = frame(parent) self.mycontainer1.pack() self.button1 = button(self.mycontainer1) self.button1.configure(text="ok", background= "green") self.button1.pack(side=left) self.button1.bind("<button-1>", self.button1click) # <--- no () ! def button1click(self, event): self.button2 = button(self.mycontainer1, text="lol") self.button2.bind("<button-1>", self.button1click) self.button2.pack() root = tk() myapp = myapp(root) root.mainloop()
bind()
expects callable , expects argument.
if pass self.button1click()
, pass none
, because returned call.
as call performed clickable object, not supposed call yourself.
so, next step: pass self.button1click
, , clock button. given "thing" tried called event object argument, fails, because method not prepared that.
so have 2 options:
- either modify method can called
event
object, suchdef button1click(self, event):
, - or wrap in lambda call:
lambda event: self.button1click()
.
in latter case, give bind()
method callable accepts 1 argument, , call wanted @ time of calling (thus ()
).
Comments
Post a Comment