动态更改标签值与全局变量tkinter

时间:2016-12-15 18:36:50

标签: python python-2.7 python-3.x user-interface tkinter

使用此代码,我想根据全局变量LED 1和LED 2动态更改标签L3和L4,但不能使其与我的代码一起使用,任何人都对我有好的建议吗? 我知道它不会工作,因为每次我开始运行代码时,方法init只运行一次。 我在使用root.after之前阅读了一些建议,但是我无法使用我的代码。

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

1 个答案:

答案 0 :(得分: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()