Reputation: 335
I am trying to call a function on button press in kivy, that is located in a different class screen than the button is located in. I tried running the function in the app class as well and ran into issues there. Here is the class where the function I am trying to call lies:
# 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'
And here is the button that I am trying to call it with:
<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()
What do I have to prefix ChangePic() with in order to call it from this location?
Alternatively- is there a way to easily work with the buttons inside of the LandingScreen class from inside of the InputScreen class?
Thanks!
Upvotes: 2
Views: 5465
Reputation: 286
You can create a variable in your App Class:
some_variable = LandingScreen()
and then in your button call ChangePic() like this:
on_release: app.some_variable.ChangePic()
Also, this can help you: StackOverflow, Kivy's Google Group, Introduction to properties
Upvotes: 4