Kivy - Parsing structure to widget

 ̄綄美尐妖づ 提交于 2020-01-07 07:49:06

问题


I'm having issues with parsing a data structure to a widget in Kivy, which would then access the structure and be able to show a value on the screen be updated continuously via a clock interval (not sure of a better to do this yet).

I have highlighted the issues in the (non-working) code below:

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

My aim is to have the testVal constantly updating on the screen by accessing that data structure, however I am currently unable to achieve this, can you please advise?


回答1:


In your __init__ method you're passing testStructTable and then you're trying to access self.testStructTable which does not exist untill you explicitly make an assignment:

from kivy.app import App
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)

        print(testStructTable)
        self.testStructTable = testStructTable
        print(self.testStructTable)


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()


来源:https://stackoverflow.com/questions/43299280/kivy-parsing-structure-to-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!