Passing down arguments from App to Children Classes
Question:
I’ve initialized my primary Kivy app class with an argument. Let’s call it x
. I need to pass this x
value down to other classes in the app. How do I go about doing that?
For example, I have setup another GridLayout
class to take the value x
in it’s __init__
too. I tried to call this GridLayout
in the main App
but it returns this following error.
TypeError: __init__() missing 1 required positional argument: 'x'
I’ve supplied the value for sure.
Here’s the total code.
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty
class MainWidget(GridLayout):
x= StringProperty('')
def __init__(self, x, **kwargs):
super(MainWidget, self).__init__(**kwargs)
self.x= x
class App(App):
def __init__(self, x) -> None:
App.__init__(self)
self.x= x
self.run()
def build(self):
App.title = 'Title'
return MainWidget(self.x)
if __name__ == '__main__':
app = App(x='Value of X')
Not able to figure how to pass down arguments. If this is not feasible, can someone tell me what would be the right way to approach this problem?
Answers:
Have you tried instantiating the main class and passing the value to gridLayout?
x = "username:blessedcoder"
myapp = App(x)
widget = MainWidget(myapp.x)
If you are using StringProperty
, then you can just reference that property when you create the instance of your App
or your MainWidget
. Kivy will automatically handle arguments like x='Value of X'
if that class has a property named x
. Note that every Widget
already has an x
property, so that is a bad choice of property name. Here is a modified version of your code that uses the above suggestions:
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty
from kivy.uix.label import Label
class MainWidget(GridLayout):
x_val = StringProperty('') # do not make another x property
def __init__(self, **kwargs):
super(MainWidget, self).__init__(**kwargs)
self.cols = 1
Clock.schedule_once(self.add_label)
def add_label(self, dt):
self.add_widget(Label(text=self.x_val))
class App(App):
x = StringProperty('')
def build(self):
App.title = 'Title'
return MainWidget(x_val=self.x)
if __name__ == '__main__':
app = App(x='Value of X')
app.run()
I’ve initialized my primary Kivy app class with an argument. Let’s call it x
. I need to pass this x
value down to other classes in the app. How do I go about doing that?
For example, I have setup another GridLayout
class to take the value x
in it’s __init__
too. I tried to call this GridLayout
in the main App
but it returns this following error.
TypeError: __init__() missing 1 required positional argument: 'x'
I’ve supplied the value for sure.
Here’s the total code.
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty
class MainWidget(GridLayout):
x= StringProperty('')
def __init__(self, x, **kwargs):
super(MainWidget, self).__init__(**kwargs)
self.x= x
class App(App):
def __init__(self, x) -> None:
App.__init__(self)
self.x= x
self.run()
def build(self):
App.title = 'Title'
return MainWidget(self.x)
if __name__ == '__main__':
app = App(x='Value of X')
Not able to figure how to pass down arguments. If this is not feasible, can someone tell me what would be the right way to approach this problem?
Have you tried instantiating the main class and passing the value to gridLayout?
x = "username:blessedcoder"
myapp = App(x)
widget = MainWidget(myapp.x)
If you are using StringProperty
, then you can just reference that property when you create the instance of your App
or your MainWidget
. Kivy will automatically handle arguments like x='Value of X'
if that class has a property named x
. Note that every Widget
already has an x
property, so that is a bad choice of property name. Here is a modified version of your code that uses the above suggestions:
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty
from kivy.uix.label import Label
class MainWidget(GridLayout):
x_val = StringProperty('') # do not make another x property
def __init__(self, **kwargs):
super(MainWidget, self).__init__(**kwargs)
self.cols = 1
Clock.schedule_once(self.add_label)
def add_label(self, dt):
self.add_widget(Label(text=self.x_val))
class App(App):
x = StringProperty('')
def build(self):
App.title = 'Title'
return MainWidget(x_val=self.x)
if __name__ == '__main__':
app = App(x='Value of X')
app.run()