将结构解析为小部件

将结构解析为小部件

本文介绍了Kivy-将结构解析为小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

推荐答案

在您的__init__方法中,您传递了testStructTable,然后您尝试访问的self.testStructTable,直到您显式地创建一个self.testStructTable作业:

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

这篇关于Kivy-将结构解析为小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:18