[Tutor] parameter passing
Magnus Lycka
magnus@thinkware.se
Sat, 19 Oct 2002 18:53:59 +0200
I'm not a Tkinter expert, but I think I know
enough to say something here...
At 15:45 2002-10-18 -0400, andy surany wrote:
>Maybe I'm going nuts......
>
>THIS WORKS:
>class aaa:
> def whowantstoknow:
> input1=3D"'"+askstring('Enter your name','Name?')+"'"
> =
Button(self,text=3D'name',command=3Dself.update_name()).pack(side=3DLEFT)
> def update_name(self):
> # do some stuff.....
Really? Must be some freak accident.
When you define a widget like a button, you should tell
the button what function to call in case of an event.
For instance "command=3Dself.update_name". Note the lack of ().
What you typed--"command=3Dself.update_name()"--means that you
run the self.update_name when you define the button in
"whowantstoknow" and that you assign the return value from
the update_name function as the callback function for the button.
This is probably wrong (unless update_name returns a function.)
>
>THIS DOES NOT (in fact, the button statement - and any other button=20
>statement is ignored. But there is no error.):
>
>class aaa:
> def whowantstoknow:
> input1=3D"'"+askstring('Enter your name','Name?')+"'"
>=20
>Button(self,text=3D'name',command=3Dself.update_name(input1)).pack(side=3DL=
EFT)
> def update_name(self,input1):
> print 'name is ',input1
>
The simple way to avoid parameter passing here is to
store inputl as an instance attribute, i.e. self.inputl.
You CAN pass params in callbacks, but that makes the code a bit
more complex. Let's save that for later...
--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se