假设我有一个计算点击次数的简单小部件:
<ClickCounter>: # BoxLayout
amount: 1
Label:
text: amount
Button:
text: '+1'
on_release: root.amount += 1
我想在我的应用程序的其他地方使用此小部件,例如:
<ParentWidget>: # BoxLayout
Label:
text: 'The widget below will count your clicks:'
ClickCounter:
id: counter
在我的python代码中,我希望执行ParentWidget(amount=5)
,即我希望ClickCounter
内的ParentWidget
从5开始计数。我该怎么做
这会起作用,但很难看,会在下一帧更新值:
class ParentWidget(BoxLayout):
def __init__(self, *args, **kwargs):
amount = kwargs.pop('amount', 0)
super(ParentWidget, self).__init__(*args, **kwargs)
Clock.schedule_once(lambda dt: self.ids.counter.amount = amount)
或者类似的东西,但该属性仅用于初始化,这同样难看:
<ParentWidget>
amount: 0
# …
ClickCounter:
amount: root.amount
没有kv语言语法用于将KWARG传递给作为kv规则一部分实例化的小部件。使用属性做您想做的事情,或者发明一个不同的控制流,或者两者兼而有之
相关问题 更多 >
编程相关推荐