1

I am giving this command in ubuntu

def gui_c(self):   
    self.button1=Button(app,text="Search",command=self.search_())
    self.button1.grid()

I want to search_() function by clicking on this button.But before clicking this function has been called and self.button1.grid() is not executed. Please help.

1
  • just add the word lambda before the function name. Something like this: command=lambda:self.search_() Commented Apr 20, 2016 at 3:41

2 Answers 2

5

To pass the function instead of executing it, get rid of the () brackets, so use command=self.search_ instead of command=self.search_()

This is python's way of referring to the function itself. For example:

>>> def foo():
...     print("Spam eggs bacon and spam")
... 
>>> foo()
Spam eggs bacon and spam

>>> foo
<function foo at 0x7f4dac4ec2a8>

>>> a = foo
>>> a
<function foo at 0x7f4dac4ec2a8>

>>> a()
Spam eggs bacon and spam
Sign up to request clarification or add additional context in comments.

Comments

1

You just need to write:

def gui_c(self):   
    self.button1=Button(app,text="Search",command=self.search_)
    self.button1.grid()

THis is a doubt that I had too, when I was new to Tkinter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.