簡體   English   中英

使用MQTT更新Tkinter GUI?

[英]Using MQTT to update Tkinter GUI?

我有一個虛擬燈泡(Tkinter GUI),如果燈泡開或關,我有兩個單獨的圖像。 我試圖弄清楚如何使用MQTT(paho mqtt)打開或關閉此虛擬燈泡。 我能夠發布和訂閱主題,並且可以使用接收到的消息(“打開”或“關閉”)來確定要顯示的圖像,我只是無法弄清楚如何在收到新消息時進行更新。

這是我創建Tkinter GUI的地方

#!/usr/bin/python3

from tkinter import *
from PIL import ImageTk, Image
import os
import sys

root = Tk()
root.geometry("768x1024")

if sys.argv[1]:
    if sys.argv[1] == "On":
        img = ImageTk.PhotoImage(Image.open("light-on.jpg"))
    else:
        img = ImageTk.PhotoImage(Image.open("light-off.jpg"))

panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

這是我訂閱該主題並檢索消息的地方。

#! /usr/bin/python3
import paho.mqtt.client as mqtt #import the client1
import time
import subprocess
import tkinter as tk
import random

def on_message(client, userdata, message):
    print("message received " ,str(message.payload.decode("utf-8")))
    print("message topic=",message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)
    subprocess.call(['./pythongui.py',message.payload.decode()])

broker_address="test.mosquitto.org"

print("creating new instance")
client = mqtt.Client() #create new instance
client.on_message=on_message #attach function to callback

print("connecting to broker")
client.connect(broker_address) #connect to broker

print("Subscribing to topic","house/bulbs/bulb1")
client.subscribe("house/bulbs/bulb1")

client.loop_forever()

我知道我可能不應該創建子流程,這可能是完全錯誤的,但是我不知道該怎么做。 任何提示將不勝感激。

##! /usr/bin/python3
import paho.mqtt.client as mqtt #import the client1
from tkinter import *
from PIL import ImageTk, Image
import os

#Get the window and set the size
window = Tk()
window.geometry('320x200')

#Load both the images
img_on = ImageTk.PhotoImage(Image.open("light-on.jpg"))
img_off = ImageTk.PhotoImage(Image.open('light-off.jpg'))

panel = Label(window, image = img_on)
panel.pack(side = "bottom", fill = "both", expand = "yes")

# This is the event handler method that receives the Mosquito messages
def on_message(client, userdata, message):
    msg = str(message.payload.decode("utf-8"))
    print("message received " , msg)

    #If the received message is light on then set the image the the ‘light on’ image
    if msg == "Study/HUE_STATE/on":
        panel.configure(image=img_on)
        print("****ON")
        panel.update()
    #If the received message is light off then set the image the the ‘light off’ image
    elif msg == "Study/HUE_STATE/off":
        panel.configure(image=img_off)
        print("****OFF")         
        panel.update()

broker_address="test.mosquitto.org"

print("creating new instance")
client = mqtt.Client() #create new instance
client.on_message=on_message #attach function to callback

print("connecting to broker")
client.connect(broker_address) #connect to broker

print("Subscribing to topic","home")
client.subscribe("home")

 #Start the MQTT Mosquito process loop
client.loop_start() 

window.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM