I've been working with this code for a days now.
I cant make it work, my goal is to pass an arguments from a function from a different CLASS to another function in a different CLASS without affecting the 'self'
Please see sample code.
PY CODE:
`
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button
#Custom Button
class PassdataButton(Button):
def on_release(self):
ThisScreen.getters(datas=['5','3'])
class ThisScreen(Screen):
def getters(self,datas):
self.ids.gettersBox.text = f"There is {datas[0]} Apple's and {datas[0]} Banana's"
class SM(ScreenManager):
pass
kv_file = Builder.load_file('getData.kv')
class projectApps(App):
def build(self):
return kv_file
if __name__ == "__main__":
projectApps().run()
`
KV CODE:
SM:
ThisScreen:
name: 'ThisScreen'
<ThisScreen>:
canvas.before:
canvas:
Rectangle:
size: self.size
source: 'im-502378444.jpg'
BoxLayout:
padding: 10
orientation: 'vertical'
TextInput:
id: gettersBox
BoxLayout:
size_hint_y: .3
orientation: 'horizontal'
Button:
text: 'GetterButton'
on_release: root.getters(datas = 'this data is from GetterButton')
#CustomButton
PassdataButton:
text: "Apple's & Banana's"
Everytime I run the code and click the Custom Button I created, it's getting me an error
ThisScreen.getters(datas=['5','3'])
TypeError: getters() missing 1 required positional argument: 'self'
When I add a value for the missing positional argument for self like ThisScreen.getters(self=True, datas=['5','3'])
its giving me different error and I cant access/call anymore other elements under the the getters() function.
self.ids.gettersBox.text = f"There is {datas[0]} Apple's and {datas[0]} Banana's"
AttributeError: 'bool' object has no attribute 'ids'
Hope you can help me with this simple code.
Thanks a bunch!
You have two options.
1. You can pass it direct from your kv file. Since you're already on the ThisScreen
screen, you can just delete your on_relase
method in python and call the function inside .kv
from root like this:
PassdataButton:
text: "Apple's & Banana's"
on_release: root.getters(datas=['5', '3'])
2. Or if you want to use python for this, you will have to get your running app, the screen manager, then jump to the screen with the desired function, and only there you can call your getter:
class PassdataButton(Button):
def on_release(self):
App.get_running_app().root.get_screen('ThisScreen').getters(datas=['5', '3'])
# App -> App instance
# get_running_app() -> current running
# root -> Screen Manager
# get_screen('ThisScreen') -> Desired screen