2016-11-01 3 views
0

결국 labels.text는 센서 데이터를 표시해야합니다. 데이터는 별도의 프로세스 내에서 생성되며 파이프를 통해 kivy로 전송됩니다. 현재 매우 간단한 예제를 구현하려고합니다. 그러나, 나는 동적으로 생성 된 라벨의 업데이트 개념의 주위에 내 머리를하지 않습니다kivy : 동적으로 생성 된 레이블의 레이블 텍스트 변경

from math import sin 

""" 
Activate the touch keyboard. It is important that this part is on top 
because the global config should be initiated first. 
""" 
from kivy.config import Config 
Config.set('kivy', 'keyboard_mode', 'multi') 

from kivy.app import App 

from kivy.clock import Clock 
# The Builder is used to define the main interface. 
from kivy.lang import Builder 

from kivy.properties import StringProperty, ObjectProperty, NumericProperty 
from kivy.uix.label import Label 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.stacklayout import StackLayout 


import numpy as np 

class MainScreen(Screen): 
    pass 


class DataScreen(Screen): 

    def __init__(self, **kwargs): 
     layout = StackLayout() 
     super(DataScreen, self).__init__(**kwargs) 
     self.n_probes = 8 
     self.label_text = [] 
     for i in range(self.n_probes): 
      self.label_text.append(StringProperty()) 
      self.label_text[i] = str(i) 

     for i in range(self.n_probes): 
      l = Label(id='l_{}'.format(i), 
         name='l_{}'.format(i), 
         text=self.label_text[i], 
         font_size='60sp', 
         height=20, 
         width=20, 
         size_hint=(0.5, 0.2)) 
      self.ids.stack.add_widget(l) 

    def change_text(self): 
     for i in range(self.n_probes): 
      self.label_text[i] = str(2) 



Builder.load_file('phapp.kv') 

class MyApp(App): 
    """ 
    The settings App is the main app of the pHBot application. 
    It is initiated by kivy and contains the functions defining the main interface. 
    """ 


    def build(self): 
     """ 
     This function initializes the app interface and has to be called "build(self)". 
     It returns the user interface defined by the Builder. 
     """ 

     sm = ScreenManager() 
     sm.add_widget(MainScreen()) 
     sm.add_widget(DataScreen()) 
     # returns the user interface defined by the Builder 
     return sm 


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

.kv 파일 : 레이블이 버전으로 업데이트되지 않습니다

<MainScreen>: 
    name: 'main' 
    BoxLayout: 
     orientation: 'vertical' 
     Button: 
      text: 'Go to data' 
      font_size: 40 
      on_release: app.root.current = 'data' 
     Button: 
      text: 'Exit' 
      font_size: 40 
      on_release: app.stop() 

<DataScreen>: 
    name: 'data' 
    StackLayout: 
     id: stack 
     orientation: 'lr-tb' 
    BoxLayout: 
     Button: 
      size_hint: (0.5, 0.1) 
      text: 'Update' 
      font_size: 30 
      on_release: root.change_text() 
     Button: 
      size_hint: (0.5, 0.1) 
      text: 'Back to main menu' 
      font_size: 30 
      on_release: app.root.current = 'main' 

. 어떤 아이디어?

답변

0

이 버전이 작동합니다. 나는 아직도 첫 번째 것이 효과가 없었던 이유는 아직 없다. 어쩌면 범위가있는 무언가에 가야할까요? .kv 파일과 .py 파일 (예 : SO post)에 정적으로 코딩하면 모든 것이 잘됩니다.

from math import sin 

""" 
Activate the touch keyboard. It is important that this part is on top 
because the global config should be initiated first. 
""" 
from kivy.config import Config 
Config.set('kivy', 'keyboard_mode', 'multi') 

from kivy.app import App 

from kivy.clock import Clock 
# The Builder is used to define the main interface. 
from kivy.lang import Builder 

from kivy.properties import StringProperty, ObjectProperty, NumericProperty 
from kivy.uix.label import Label 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.stacklayout import StackLayout 


import numpy as np 

class MainScreen(Screen): 
    pass 


class DataScreen(Screen): 

    def __init__(self, **kwargs): 
     layout = StackLayout() 
     super(DataScreen, self).__init__(**kwargs) 
     self.n_probes = 8 
     self.label_text = [] 
     for i in range(self.n_probes): 
      self.label_text.append(StringProperty()) 
      self.label_text[i] = str(i) 
     self.l = [] 
     for i in range(self.n_probes): 
      self.l.append(Label(id='l_{}'.format(i), 
          text='Start {}'.format(i), 
          font_size='60sp', 
          height=20, 
          width=20, 
          size_hint=(0.5, 0.2))) 
      self.ids.stack.add_widget(self.l[i]) 

    def change_text(self): 
     for i in range(self.n_probes): 
      self.l[i].text = 'Update' + str(2) 


Builder.load_file('phapp.kv') 

class MyApp(App): 
    """ 
    The settings App is the main app of the pHBot application. 
    It is initiated by kivy and contains the functions defining the main interface. 
    """ 


    def build(self): 
     """ 
     This function initializes the app interface and has to be called "build(self)". 
     It returns the user interface defined by the Builder. 
     """ 

     sm = ScreenManager() 
     sm.add_widget(MainScreen()) 
     sm.add_widget(DataScreen()) 
     # returns the user interface defined by the Builder 
     return sm 


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


<MainScreen>: 
    name: 'main' 
    BoxLayout: 
     orientation: 'vertical' 
     Button: 
      text: 'Go to data' 
      font_size: 40 
      on_release: app.root.current = 'data' 
     Button: 
      text: 'Exit' 
      font_size: 40 
      on_release: app.stop() 

<DataScreen>: 
    name: 'data' 
    StackLayout: 
     id: stack 
     orientation: 'lr-tb' 
    BoxLayout: 
     Button: 
      size_hint: (0.5, 0.1) 
      text: 'Update' 
      font_size: 30 
      on_release: root.change_text() 
     Button: 
      size_hint: (0.5, 0.1) 
      text: 'Back to main menu' 
      font_size: 30 
      on_release: app.root.current = 'main' 
관련 문제