2017-04-10 50 views
0

仍然在這裏學習Python和Kivy,並繼續從this問題,並且將數據結構(字典)解析爲屏幕有點困惑。Kivy - 解析對屏幕結構的引用

什麼是解析所述數據結構到屏幕的最佳方式是什麼?

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

     self.testStructTable = testStructTable 

     sm = ScreenManager() 
     # This line here to parse the data structure? 
     sm.add_widget(MainScreen(name='MainScreen'), testStructTable) 
     self.add_widget(sm) 

     Clock.schedule_interval(MainScreen.update_test_val(testStructTable), 1/60) 

class MainScreen(Screen): 
    testVal = NumericProperty(0) 

    def __init__(self, **kwargs): 
     super(MainScreen, self).__init__(**kwargs) 
     # would like to parse the testStructTable here? 
     # self.testStructTable = testStructTable 

    def update_test_val(dt, self): 
     self.testVal = testStructTable['testVal'] 

回答

0

通過讓一個setter到屏幕上,而不是實現了這個:

class TestWidget(RelativeLayout): 

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

     self.testStructTable = testStructTable 
     screen = MainScreen(name='MainScreen') 
     screen.set_table(self.testStructTable)   

     sm = ScreenManager() 
     sm.add_widget(screen) 
     self.add_widget(sm) 

     Clock.schedule_interval(MainScreen.update_test_val, 1/60) 

class MainScreen(Screen): 
    testVal = NumericProperty(0) 
    testStructTable = {} 

    def __init__(self, **kwargs): 
     super(MainScreen, self).__init__(**kwargs) 

    def set_table(self, testStructTable): 
     self.testStructTable = testStructTable 

    def update_test_val(self, dt): 
     self.testVal = self.testStructTable['testVal'] 

雖然如果有人有這樣做的更好的方法,我很想看看它如何能代替完成。