0

我想知道在创建小部件期间在哪里可以查看和使用数据上给出的参数。

问题:看起来__init__没有信息,所以必须在调用之后,所以我想知道什么时候可以使用我的自定义参数。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.factory import Factory as F

Builder.load_string('''
<RV>:
    viewclass: 'CustomLabel'
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
''')

class CustomLabel(F.Label):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        print(kwargs, "why can't I see the stuff from data ???")

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x), "new_parameter":"idk where am I"} for x in range(100)]


class TestApp(App):
    def build(self):
        return RV()

if __name__ == '__main__':
    TestApp().run()
4

1 回答 1

0

的属性data没有在 的__init__()方法中设置CustomLabel,它们是在创建基本之后单独设置的CustomLabel。您可以通过将on_text()方法添加到您的CustomLabel:

class CustomLabel(F.Label):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        print(kwargs, "why can't I see the stuff from data ???")

    def on_text(self, instance, new_text):
        print('CustomLabel', id(instance), 'text set to:', new_text)
于 2021-08-13T14:32:31.410 回答