通过 Kivy 按钮在不同 Class 中调用函数
Calling Function in a Different Class Through Kivy Button
我正在尝试在 kivy 中调用按钮按下功能,它位于与按钮所在的不同 class 屏幕中。我尝试 运行 应用程序中的功能 class 以及 运行 那里的问题。这是我要调用的函数所在的 class:
# 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 class 内部轻松使用 LandingScreen class 内部的按钮?
谢谢!
您可以在您的应用程序中创建一个变量Class:
some_variable = LandingScreen()
然后在您的按钮中像这样调用 ChangePic():
on_release: app.some_variable.ChangePic()
此外,这可以帮助您:Whosebug, Kivy's Google Group, Introduction to properties
我正在尝试在 kivy 中调用按钮按下功能,它位于与按钮所在的不同 class 屏幕中。我尝试 运行 应用程序中的功能 class 以及 运行 那里的问题。这是我要调用的函数所在的 class:
# 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 class 内部轻松使用 LandingScreen class 内部的按钮?
谢谢!
您可以在您的应用程序中创建一个变量Class:
some_variable = LandingScreen()
然后在您的按钮中像这样调用 ChangePic():
on_release: app.some_variable.ChangePic()
此外,这可以帮助您:Whosebug, Kivy's Google Group, Introduction to properties