如何在Kivy中将自我作为参数传递给Spinner的bing函数

时间:2017-10-16 14:40:26

标签: python spinner kivy

我想将一个函数调用到一个微调器,我需要在函数中使用self参数但是当我尝试执行该程序时,我得到了这个错误:  TypeError:graphTypeSelected()只需要3个参数(给定2个)

这是我绑定函数的方式:     spinGraphType.bind(text = GraphPopup.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]()

如何将self参数传递给graphTypeSelected以便我可以调用该类中的其他函数? 谢谢。

1 个答案:

答案 0 :(得分:2)

详情请参阅以下示例。

实施例

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()

输出

Button added into Spinner