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

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

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

# 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 类内部的按钮?

谢谢!

最佳答案

您可以在应用程序类中创建一个变量:

some_variable = LandingScreen()

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

on_release: app.some_variable.ChangePic()

此外,这可以帮助您:StackOverflow , Kivy's Google Group , Introduction to properties

关于python - 通过Kivy Button调用不同类中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45024151/

相关文章:

Python 的 sqlite3.register_converter() 不会将 double 转换为 Decimal

python - 使用 python 和 sqlite3 插入的性能

函数中的 C 错误 : unknown type name 'FILE' ,

php - 将默认参数作为 null 发送并使用函数中设置的默认值

python - 在django中的渲染函数中访问args

python - matplotlib 中的 rc 键列表。刻度标签旋转

python-3.x - Pycharm:如何从字典内部重构代码

python - 如何在Python中从纬度和经度获取带有关联邮政编码的数据框?

python - 使用 Darts 进行多协变量时间序列预测

BASH 将变量传递给函数