gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:33:33 24 4
gpt4 key购买 nike

我试图在 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 按钮调用不同类中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45024151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com