2016-10-22 7 views
0

에 내가 여기 OSC 라이브러리를 사용하여 파이썬에서 서비스를 쓴 것은 내 코드입니다 :변경 버튼 텍스트는 동적으로 kivy

def __init__(self, * * kwargs): 

     super(ElectronApp, self).__init__(* * kwargs) 

     self.electronController = ElectronController() 

     self.button = ElectronTHView() 

    def build(self): 
     return Builder.load_file('ElectronApp.kv') 


    def build(self): 

     self.button = ElectronTHView() 

     self.service = None 

     self.start_service() 

     osc.init() 

     oscid = osc.listen(port = 3002) 

     osc.bind(oscid, self.display_message, '/message') 

     osc.bind(oscid, self.date, '/date') 

     Clock.schedule_interval(lambda * x: osc.readQueue(oscid), 0) 

     self.root = Builder.load_file('ElectronApp.kv') 

     return self.root 
    .... 


    def date(self, message, * args): 

     print(message[2]) 

     # here i get message from my service and i want to update text on button 
     self.button.ids.tem.text = message[2] 

class ElectronAppMain(BoxLayout): 

    pass 

class ElectronAppMainView(TabbedPanel): 

    pass 

class ElectronAppBottomBar(BoxLayout): 

    pass 


class ElectronButton(Button): 

    pass 

class ElectronTHView(BoxLayout): 
    pass 

* .KV 파일 코드 :

ElectronAppMain: 

<ElectronAppMain>: 

    padding: "-2dp" 

    orientation: "vertical" 

    canvas.before: 

     Rectangle: 

      size: self.size 

      pos: self.pos 

      source: "background.png" 

    ElectronAppMainView: 

    ElectronAppBottomBar: 


<ElectronAppBottomBar>: 
    size_hint: 1, None 
    height: "50dp" 
    padding: "0dp" 
    orientation: "horizontal" 
    spacing: "0dp" 
    ElectronButton: 
     text: "STOP" 
     size_hint: 1, None 
     height: "50dp" 
     on_release: 
    ElectronButton: 
     text: "EXIT" 
     size_hint: 1, None 
     height: "50dp" 
     on_release: 

<ElectronAppMainView>: 
    tab_pos: "top_left" 
    do_default_tab: False 
    tab_height: "50dp" 
    tab_width: root.width/3 
    background_image: 
     font_size: "12dp" 
     background_normal: 
     background_down: 
     text: "T/H" 
     font_name: "fonts/Roboto-Medium.ttf" 
     ElectronTHView: 

    TabbedPanelItem: 
     font_size: "12dp" 
     background_normal: 
     background_down: 
     text: "CUSTOM" 
     font_name: "fonts/Roboto-Medium.ttf" 

    TabbedPanelItem: 
     font_size: "12dp" 
     background_normal: 
     background_down: 
     text: "PATTERN" 
     font_name: "fonts/Roboto-Medium.ttf" 

<ElectronTHView>: 
    orientation: "vertical" 
    spacing: "5dp" 
    padding: "10dp" 


    ElectronButton: 
     id: temp 
     background_normal: .... 
     background_down: 
     size_hint: 1, .5 
     height: self.height 
     text: "Temperature" 
     on_release: 
    ElectronButton: 
     background_normal: 
     background_down: 
     size_hint: 1, .5 
     height: self.height 
     text: "Humidity" 
     on_release: 

내가 더 많은 옵션이 있지만 아무것도 시도했다.

: 내가
def date(self, message, *args): 

    print(message[2]) 
    #here i get message from my service and i want to update text on button 
    self.button.ids.tem.text = message[2] 

에 텍스트를 업데이트하려고하면 .. 내가 같은 오류가 발생합니다
Sat Oct 22 16:35:27 2016 
OSC callback /date caused an error: 'super' object has no attribute '__getattr__' 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/dist-packages/kivy/lib/osc/OSC.py", line 288, in dispatch 
    callbackfunction(message, source) 
    File "main.py", line 128, in date 
    self.button.ids.temp.text = '1' 
    File "kivy/properties.pyx", line 757, in kivy.properties.ObservableDict.__getattr__ (kivy/properties.c:11882) 
AttributeError: 'super' object has no attribute '__getattr__' 
--------------------- 
[INFO ] [Base  ] Leaving application in progress... 
[ERROR ] [OSC   ] Error in Tuio recv() 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/dist-packages/kivy/lib/osc/oscAPI.py", line 229, in run 
    message = self.socket.recv(65535) 
error: [Errno 4] Interrupted system call 

저는 새로운 kivy ... 도움을 위해 미리 감사드립니다!

+1

질문의 형식을 올바르게 지정하십시오. 지금은 읽을 수 없습니다. – agg3l

+0

안녕하세요! 나는 이걸했다! – Clark

+0

이렇게하면 훨씬 좋습니다. 스택 추적을 파이썬 (전체 오류 텍스트)에서 인용 할 수 있다면, 가능하다면 다른 사람이 도움을 줄 수있는 방법이 훨씬 더 간단 할 것입니다. – agg3l

답변

0

먼저 안녕하세요. 내 문제의 해결책을 공유하는 것을 잊어 버렸습니다. 그것의 아주 간단했다 -하지만 난 당황에 있었고, 한 번에 내가하는 모든 일을 이해하지 - 난 그냥 * 이러한 모드에서 .kv 파일이 다시 한 : 다시 모든

ElectronAppMain: 

<ElectronAppMain>: 

    padding: "-2dp" 
    orientation: "vertical" 
    canvas.before: 
     Rectangle: 
      size: self.size 
      pos: self.pos 
      source: "background.png" 
    ElectronMainView: 

     tab_pos: "top_left" 
     do_default_tab: False 
     tab_height: "50dp" 
     tab_width: root.width/3 
     TabbedPanelItem: 
      font_size: "12dp" 
      text: "T/U" 
      font_name: "fonts/Roboto-Medium.ttf" 
      ElectronTHView: 
       orientation: "vertical" 
       spacing: "5dp" 
       padding: "5dp" 

       ElectronLabel: 
        id: date 
        color: 0,0,0,0.54 
        halign: "center" 
        font_size: "16dp" 
        size_hint: 1, 1 
        height: self.texture_size[1] 
        text_size: self.width, None 
        font_name: "fonts/Roboto-Regular.ttf" 

       ElectronButton: 
        id: test 
        on_press: app.send() 


     TabbedPanelItem: 
      font_size: "12dp" 
      text: "CUSTOM" 
      font_name: "fonts/Roboto-Medium.ttf" 

     TabbedPanelItem: 
      font_size: "12dp" 

      text: "PATTERN" 
      font_name: "fonts/Roboto-Medium.ttf" 

감사합니다!

0

"on_release"이벤트를 통해 날짜 (self, message, * args) 함수로 "ElectronButton"위젯의 텍스트를 업데이트하려고합니까? 비록 그것이 중요하지 않더라도,이 예제는 그럼에도 불구하고 당신을 도울 수 있습니다 :

먼저, .kv 파일로부터 적절히 호출 될 수 있도록 date()를 올바른 위치에 두어야합니다.

class ElectronAppBottomBar(BoxLayout): 

    def date(self, widget, message, *args): 
     widget.text = message[2] 

그리고 .kv 파일에 다음에서 : 예를 들어

<ElectronAppBottomBar>: 
    size_hint: 1, None 
    height: "50dp" 
    padding: "0dp" 
    orientation: "horizontal" 
    spacing: "0dp" 

    ElectronButton: 
     text: "STOP" 
     size_hint: 1, None 
     height: "50dp" 
     on_release:root.date(self,message) #or app.date(self,message) if date is definded at the App level 

이건 그냥 예입니다. 그것이 올 때 : 당신이 어떤 클래스 내에서 따라

self.button.ids.tem.text = message[2] 

, self.ids.tem.text = 메시지 [2] 또는 self.parent.ids.tem.text = 메시지 같은 물건 [2] 수 당신이 원하는 것을 얻기에 충분합니다. 위젯 트리를 탐색하는 방법을 official documentation에서 확인하십시오. 희망이 도움이 될 것입니다.

관련 문제