2017-04-08 1 views
0

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 

테스트. kv

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

목표는 해당 데이터 구조에 액세스하여 화면에서 지속적으로 testVal을 업데이트하는 것입니다. 그러나 현재이를 달성 할 수 없으므로 조언 해주십시오. 당신의 __init__ 방법에서

답변

1

당신은 testStructTable을 전달하는 다음 당신은 당신이 명시 적으로 할당하기 전까지 존재하지 않는 액세스 self.testStructTable에 노력하고 : 일 것

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

, 감사합니다! 또한 해당 testVal을 지속적으로 화면에서 업데이트하는 방법을 알려줄 수 있습니까? – Rekovni

+0

'kivy.clock'모듈과 결합 된 속성을 사용하는 것은 올바른 방법입니다. Kivy의 등록 정보는 관측자 패턴을 구현하므로 변경 사항이 즉시 위젯에 반영 될 수 있습니다. 예제는 [here] (http://www.gurayyildirim.com.tr/kivy-course-5-properties-and-clock-definitions-1191.html)를 참조하십시오. – Nykakin