Newbie question, problem with functions

Peter Otten __peter__ at web.de
Tue Sep 23 17:35:01 EDT 2003


Rickard Karlsson wrote:

> I have wrote this little program, that is supposed to execute eg. the
> Linux ls command  when you press a button. Te problem is that all the
> commands are run automaticly and they won't run when I press the
> associated button. Wath am I do wrong?

[...]
> b1 = Button(frame, text = "ls", command = self.execute('ls'))
> b2 = Button(frame, text = "ls -alh", command = self.execute('ls
> -alh'))
> b3 = Button(frame, text = "pwd", command = self.execute('pwd'))
[...]

You are in fact passing the result of self.execute(...), i. e. None, instead
of the method. Do

b3 = Button(frame, text="pwd", command=lambda: self.execute('pwd'))

instead or define wrapper methods like so:

def executeLs(self):
    self.execute("ls")

and assign them to the buttons without a trailing (), e. g.:

b1 = Button(frame, text="ls", command=self.executeLs)

Peter




More information about the Python-list mailing list