Can't pass kwargs to sub class in Kivy -- TypeError: object.__init__() takes exactly one argument (the instance to initialize)
Can't pass kwargs to sub class in Kivy -- TypeError: object.__init__() takes exactly one argument (the instance to initialize)
我对编程还很陌生,如果这里的解决方案很明显,请原谅我。我已经做了相当多的谷歌搜索,但无法解决这个问题。
我正在尝试启用 subclass 来接受 base class 可以接受的任何关键字参数,以及一些我想传递的附加参数,但我将 运行 保留在以下错误:TypeError: object.init() takes exactly one argument (the instance to initialize).
如果我注释掉未内置到基础 class 中的关键字参数(workout_title、last_completed 和 lifts)代码运行没有问题。
如何让子class接受我的额外kwargs?
class SelectionBanner(GridLayout):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
grid = GridLayout(
rows = 3,
size_hint = (.8,1)
)
workout_title_label = Label(
size_hint = (1,.33),
pos_hint = {"top": 1, "left": .5},
text = "workout tile"
)
last_completed_label = Label(
size_hint = (1,.33),
pos_hint = {"top": 1, "left": .5},
text = "last_completed"
)
lifts_label = Label(
size_hint = (1,.33),
pos_hint = {"top": 1, "left": .5},
text = "lifts"
)
button = Button(
size_hint = (.2, 1),
text = "Click me!"
)
grid.add_widget(workout_title_label)
grid.add_widget(last_completed_label)
grid.add_widget(lifts_label)
self.add_widget(grid)
self.add_widget(button)
def build_selection_page(self):
data = db.reference("/workout_templates").get()
selection_page = self.root.ids['home_screen'].ids['top_layout']
selection_page.add_widget(SelectionBanner(
cols = 2,
padding = 10,
spacing = 10,
pos_hint = {"top": 1, "left": .5},
size_hint = (1, .2),
workout_title = "Workout B",
last_completed = "Yesterday",
lifts = "Bench and stuff"
))
只需为新的关键字参数添加 Properties
,如下所示:
class SelectionBanner(GridLayout):
workout_title = StringProperty('')
last_completed = StringProperty('')
lifts = StringProperty('')
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
.
.
.
我对编程还很陌生,如果这里的解决方案很明显,请原谅我。我已经做了相当多的谷歌搜索,但无法解决这个问题。 我正在尝试启用 subclass 来接受 base class 可以接受的任何关键字参数,以及一些我想传递的附加参数,但我将 运行 保留在以下错误:TypeError: object.init() takes exactly one argument (the instance to initialize).
如果我注释掉未内置到基础 class 中的关键字参数(workout_title、last_completed 和 lifts)代码运行没有问题。
如何让子class接受我的额外kwargs?
class SelectionBanner(GridLayout):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
grid = GridLayout(
rows = 3,
size_hint = (.8,1)
)
workout_title_label = Label(
size_hint = (1,.33),
pos_hint = {"top": 1, "left": .5},
text = "workout tile"
)
last_completed_label = Label(
size_hint = (1,.33),
pos_hint = {"top": 1, "left": .5},
text = "last_completed"
)
lifts_label = Label(
size_hint = (1,.33),
pos_hint = {"top": 1, "left": .5},
text = "lifts"
)
button = Button(
size_hint = (.2, 1),
text = "Click me!"
)
grid.add_widget(workout_title_label)
grid.add_widget(last_completed_label)
grid.add_widget(lifts_label)
self.add_widget(grid)
self.add_widget(button)
def build_selection_page(self):
data = db.reference("/workout_templates").get()
selection_page = self.root.ids['home_screen'].ids['top_layout']
selection_page.add_widget(SelectionBanner(
cols = 2,
padding = 10,
spacing = 10,
pos_hint = {"top": 1, "left": .5},
size_hint = (1, .2),
workout_title = "Workout B",
last_completed = "Yesterday",
lifts = "Bench and stuff"
))
只需为新的关键字参数添加 Properties
,如下所示:
class SelectionBanner(GridLayout):
workout_title = StringProperty('')
last_completed = StringProperty('')
lifts = StringProperty('')
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
.
.
.