2016-12-15 88 views
0

使用此代碼我想根據全局變量LED 1和LED 2動態更改標籤L3和L4,但無法使其與我的代碼一起工作,任何人都可以對我有好的建議? 我知道它不會工作,因爲方法init只是運行一次,我開始運行我的代碼。 我在使用root.after之前閱讀了一些建議,但我無法使用它處理我的代碼。使用全局變量tkinter動態更改標籤值

from Tkinter import * 
import ttk 
from ttk import * 
suhu=30 
cahaya=50 
LED1='-' 
LED2='-' 
def mqttx(): 
    def on_message(mqttc,obj,msg): 
     global LED1 
     global LED2 
     print "Telah Diterima message : "+msg.payload+" topik "+msg.topic 
     r.rpush(msg.topic,msg.payload) 
     datasuhu = r.lrange("suhu",-1,-1) 
     datacahaya = r.lrange("cahaya",-1,-1) 
     if msg.topic=="suhu": 
      if float(msg.payload)<=suhu : 
       mqttc.publish("2","1",qos=0,retain=False) 
       LED1="ON" 

      elif float(msg.payload)>suhu: 
       mqttc.publish("2","0",qos=0,retain=False) 
       LED1="OFF" 
     if msg.topic=="cahaya" : 
      if float(msg.payload) <=cahaya: 
       mqttc.publish("1","1",qos=0,retain=False) 
       LED2="ON" 
      elif float(msg.payload)>cahaya: 
       mqttc.publish("1","0",qos=0,retain=False) 
       LED2="OFF" 


    mqttc.on_message = on_message 
    mqttc.connect("localhost",1883) 
    mqttc.subscribe("suhu") 
    mqttc.subscribe("cahaya") 

class Example(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent) 
     self.parent = parent 
     self.initUI() 


    def initUI(self): 

     self.parent.title("Subcriber") 
     self.style = Style() 
     self.style.theme_use("default") 
     self.pack(fill=BOTH, expand=1) 


     self.L3 =Label(self,text="LED 1 : "+LED1) 
     self.L3.pack 
     self.L3.grid() 
     self.L4 =Label(self,text="LED 2 : "+LED2) 
     self.L4.pack 
     self.L4.grid() 



def main(): 
    root = Tk() 
    root.geometry("250x150+300+300") 
    app = Example(root) 
    root.mainloop() 

if __name__ == '__main__': 
    mqttx() 
    mqttc.loop_start() 
    main() 
+0

這對於這個問題,太多的代碼。請閱讀[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) –

+0

使用'self.L3 ['text'] =「新文本」'來更改標籤中的文本。 – furas

+0

您可以使用'StringVar'作爲LED1和LED2,然後將它們分別與L3和L4的'textvariable'關聯。 – acw1668

回答

0

使用after()來調用將改變標籤中文本的函數。

我創建test()功能來測試它沒有功能mqttx()

from Tkinter import * 
import ttk 
from ttk import * 

LED1 = '-' 
LED2 = '-' 

class Example(Frame): 

    def __init__(self, parent): 
     Frame.__init__(self, parent) 
     self.parent = parent 
     self.initUI() 

     # run first time 
     self.update() 

    def initUI(self): 

     self.parent.title("Subcriber") 
     self.style = Style() 
     self.style.theme_use("default") 
     self.pack(fill=BOTH, expand=1) 

     self.L3 = Label(self,text="LED 1 : "+LED1) 
     self.L3.grid() 
     self.L4 = Label(self, text="LED 2 : "+LED2) 
     self.L4.grid() 

    def update(self): 
     #print('[DEBUG] update_text') 

     self.L3['text'] = "LED 1 : " + LED1 
     self.L4['text'] = "LED 2 : " + LED2 

     # run again after 500ms (0.5s) 
     self.after(500, self.update) 


def test(root): 
    import random  
    global LED1 
    global LED2 

    print('[DEBUG] test') 
    LED1 = str(random.randint(1,10)) 
    LED2 = str(random.randint(1,10)) 

    # run again after 500ms (0.5s) 
    root.after(500, test, root) 

def main(): 
    root = Tk() 
    root.geometry("250x150+300+300") 
    app = Example(root) 
    test(root) 
    root.mainloop() 

main() 
+0

工作就像魅力!謝謝! –