freedom
freedom

Reputation: 751

Check Buttons not firing in matplotlib

I am using this example as a guide to build a checkbox button set; however, it is not firing for me. Within a class I have a function that consists of the button:

check = CheckButtons(rax,('Button 1', 'Button 2', 'Button 3'), (True,False,False))

then I have this function within a class... where it looks like:

def clickButtons(self,label):

and in another method in the same class I am attempting to call it with the following...

self.check.on_clicked(self.clickButtons())

From this website: http://matplotlib.org/examples/widgets/check_buttons.html

matplotlib button page states:

on_clicked(func) When the button is clicked, call func with button label

A connection id is returned which can be used to disconnect

However, my current error says that:

TypeError: clickButtons() takes exactly 2 arguments (1 given)

Can someone please explain to me what is going on... how I'm I suppose to know which Button is being pressed? Thanks in advance.

Upvotes: 0

Views: 1349

Answers (1)

tacaswell
tacaswell

Reputation: 87376

The function check.on_clicked is used to register a call back for when the check box is clicked. You need to pass it, as an argument, a function object which takes one argument label. The syntax you want to use is:

check.on_clicked(my_obj.clickButtons)

There are two important things to understand here. First, in Python everything is an object. Functions are just objects that happen to have an attribute __call__. The second, is that when you bind a member function to an instance of a class, the instance object is the implicit first argument, thus my_obj.clickButtons is an object which as a function which takes one argument.

When you call check.on_clicked(self.clickButtons()) you are saying 'call the function on_clicked on the object check using the results of the call self.clickButtons() as the argument'. The call to clickButtons throws the error you see because it requires 2 arguments (the implicit self and label) and you are calling it with only one argument (the implicit self).

If you have a class A, with function f(self,b), and in instance a of A, the following two are equivalent

a.f(1)      # call member function f on a
A.f(a,1)    # call member function f on a

Upvotes: 1

Related Questions