Twistx77
Twistx77

Reputation: 67

How to pass self as argument to bing function of Spinner in Kivy

I want to call bind a function to a spinner where I need to have the self argument in the function but when I try to execute the program it I get this error: TypeError: graphTypeSelected() takes exactly 3 arguments (2 given)

This is how I bind the function: spinGraphType.bind (text = GraphPopup.graphTypeSelected)

This is my function:

def graphTypeSelected (self,spinner, text): print('Hola') btnPrueba = 
    Button(text = 'Prueba') spinner.parent.add_widget(btnPrueba) functionsLUT = 
    { "Accelerometer": self.temperatureGraph,
      "Temperature": 
       self.temperatureGraph } 
     functionsLUT[text]()

How can I pass self argument to graphTypeSelected so I can call other functions in that class? Thank you.

Upvotes: 0

Views: 238

Answers (1)

ikolim
ikolim

Reputation: 16031

Please refer to the example below for details.

Example

main.py

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner


class MyWidget(Spinner):

    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
        self.create_spinner()

    def create_spinner(self):
        self.text = "Home"
        self.values = ("Home", "Work", "Other", "Custom")
        self.size_hint = (None, None)
        self.size = (100, 44)
        self.pos_hint = {"center_x": 0.5, "center_y": 0.5}
        self.bind(text=self.graphTypeSelected)

    def graphTypeSelected(self, spinner, text):
        print("Hola")
        btnPrueba = Button(text="Prueba")
        spinner.parent.add_widget(btnPrueba)
    #     functionsLUT = {"Accelerometer": self.temperatureGraph,
    #                     "Temperature": self.temperatureGraph}
    #     functionsLUT[text]()
    #
    # def temperatureGraph(self):
    #     print("temperatureGraph called")


class TestApp(App):
    title = "Kivy Spinner Demo"

    def build(self):
        return MyWidget()


if __name__ == "__main__":
    TestApp().run()

Output

Button added into Spinner

Upvotes: 2

Related Questions