通过Kivy Button调用不同类中的函数

时间:2017-07-11 01:54:55

标签: python python-3.x function kivy kivy-language

我试图在kivy按下按钮时调用一个函数,它位于与按钮所在的不同的类屏幕中。我尝试在app类中运行该函数并在那里遇到问题。这是我试图调用的函数所在的类:

# Main screen with button layout
class LandingScreen(Screen):
    def __init__(self, **kwargs):
        super(LandingScreen, self).__init__(**kwargs)
        self.buttons = [] # add references to all buttons here
        Clock.schedule_once(self._finish_init)

    def ChangePic(self):
        self.buttons[1].background_normal = 'folder.png'

以下是我试图称之为的按钮:

<InputScreen@Screen>:
    name: 'input_sc'
    FloatLayout:
        size: 800, 480
        id: anchor_1
        Label: 
            text: "What would you like to bind to this button?"
            size_hint: (1,.15)
            text_size: self.size
            pos_hint: {'x': 0.11, 'top': 1}
            font_size: 28
            font_name: 'Montserrat-Bold.ttf'
        Button:
            root: 'landing_sc'
            id: filebutton
            size: 150, 150
            size_hint: None, None
            background_normal: 'folder.png'
            background_down: 'opacity.png'
            pos_hint: {'x': 0.11, 'top': .7}
            on_release: 
                root.manager.transition = FadeTransition()
                root.manager.transition.duration = 1.5
                app.MakeFolder()
                root.IfFolder()
                root.ChangeToSlide()

我需要为ChangePic()添加前缀以便从此位置调用它?

或者 - 有没有办法从InputScreen类内部轻松使用LandingScreen类中的按钮?

谢谢!

1 个答案:

答案 0 :(得分:3)

您可以在App Class中创建变量:

some_variable = LandingScreen()

然后在按钮中调用ChangePic(),如下所示:

on_release: app.some_variable.ChangePic()

此外,这可以帮助您:StackOverflowKivy's Google GroupIntroduction to properties