2016-10-22 155 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 

我是新的基維...在此先感謝您的幫助!

+1

請仔細格式化您的問題。現在無法讀取它 – agg3l

+0

嗨!我做到了! – Clark

+0

這種方式更好。如果你可以引用來自python的堆棧跟蹤(完整的錯誤文本),如果可用的話,對於有人來協助你來說,它會簡單得多。 – 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」小部件的文本?即使這不是重點,但這個例子可以幫助你:

首先,你應該把date()放在正確的位置,以便它可以從.kv文件中正確調用。例如:

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如何導航窗口小部件樹。希望這會有所幫助。