Kivy - 解析小部件

时间:2017-04-08 19:10:20

标签: python data-structures kivy

我遇到了将数据结构解析为Kivy中的小部件的问题,然后可以访问该结构,并能够在屏幕上显示一个值,通过时钟间隔不断更新(不确定更好的做法)这个)。

我已经突出显示了以下(非工作)代码中的问题:

main.py

from kivy.app import App
from test import TestWidget

class TestApp(App):

    def build(self):
        testStructTable = {'randomVal1': 1, 'testVal': 2, 'randomVal2': 3}

        # Issue here parsing the table like this?
        return TestWidget(testStructTable)

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

test.py

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import NumericProperty


class TestWidget(RelativeLayout):

    def __init__(self, testStructTable, **kwargs):
        super(TestWidget, self).__init__(**kwargs)
        Builder.load_file('test.kv')

        sm = ScreenManager()
        sm.add_widget(MainScreen(name='MainScreen'))
        self.add_widget(sm)

        # Error accessing the table
        print self.testStructTable

        # Have the update_test_val continuously called
        #Clock.schedule_interval(MainScreen.update_test_val(testStructTable), 1 / 60)


class MainScreen(Screen):

    def __init__(self, **kwargs):
        testVal = NumericProperty(0)

    def update_test_val(self, testStructTable):
        # Get testVal from testStructTable
        # Something like:
        # self.testVal = testStructTable.testVal + 1 ?
        self.testVal = self.testVal + 1

test.kv

<MainScreen>:
    FloatLayout:
        Label:
            text: str(root.testVal)
            font_size: 80

我的目标是通过访问该数据结构让testVal在屏幕上不断更新,但是我目前无法实现这一点,请您指教一下?

1 个答案:

答案 0 :(得分:1)

jest.mock('path/to/dispatcher.js')方法中,您传递的内容为__init__,然后您尝试访问不存在的testStructTable,直到明确作出任务:

self.testStructTable