Solution
This is a perfect place to use a lambda:
self.pushButton_3.clicked.connect(lambda: self.clearTextEdit(x))
Remember, connect
expects a function of no arguments, so we have to wrap up the function call in another function.
Explanation
Your original statement
self.pushButton_3.clicked.connect(self.clearTextEdit(x)) # Incorrect
was actually calling self.clearTextEdit(x)
when you made the call to connect
, and then you got an error because clearTextEdit
doesn't return a function of no arguments, which is what connect
wanted.
Lambda?
Instead, by passing lambda: self.clearTextEdit(x)
, we give connect
a function of no arguments, which when called, will call self.clearTextEdit(x)
. The code above is equivalent to
def callback():
return self.clearTextEdit(x)
self.pushButton_3.clicked.connect(callback)
But with a lambda, we don't have to name "callback", we just pass it in directly.
If you want to know more about lambda functions, you can check out this question for more detail.
On an unrelated note, I notice that you don't use x
anywhere in clearTextEdit
. Is it necessary for clearTextEdit
to take an argument in the first place?