gpt4 book ai didi

python - 将变量从 MQTT 传递给 Kivy

转载 作者:行者123 更新时间:2023-12-04 13:21:11 25 4
gpt4 key购买 nike

我尝试使用抽屉导航将值从 MQTT 传递到 Kivy 仪表板。

有人可以提示我如何更改变量吗?
这是我的示例代码。 MQTT 正在运行。
我使用 Python 3.6.6 和 1.10.1。

主菜单.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.garden.navigationdrawer import NavigationDrawer
from kivy.properties import StringProperty
import paho.mqtt.client as mqtt

class Drawer(NavigationDrawer):
vartext1 = StringProperty("Button")
vartext2 = StringProperty("Text")

class MainMenuApp(App):
def build(self):
return Drawer()

def on_start(self):
topic = "kivy/#"

def onConnect(client, userdata, flags, rc):
mqttc.subscribe(topic, 0)

def onMessage(client, userdata, msg):
msg.payload = msg.payload.decode("utf-8")
print ("[INFO ] [MQTT ] topic: " + msg.topic +" msg: "+ msg.payload)
if msg.topic == "kivy/button":
vartext1 = msg.payload

if msg.topic == "kivy/text":
vartext2 = msg.payload

mqttc = mqtt.Client(client_id="kivy-client", clean_session=True)
mqttc.on_connect = onConnect
mqttc.on_message = onMessage
mqttc.connect("10.0.0.104", 1883, keepalive=60, bind_address="")
mqttc.loop_start() # start loop to process callbacks! (new thread!)

if __name__ == "__main__":
MainMenuApp().run()

主菜单.kv
<Drawer>:
# Side panel
BoxLayout:
padding: 30
Button:
text: root.vartext1
# Main panel
BoxLayout:
padding: 30
Label:
text: root.vartext2

最佳答案

您可以像这样将“self”参数传递给 mqtt.Client 构造函数:

parameters = {'self': self}
mqttc = mqtt.Client(client_id="kivy-client", clean_session=True, userdata = parameters)

现在在 onMessage 过程中,您可以访问该参数:
userdata['self'].vartext1 = msg.payload

所以你的 mainmenu.py 文件应该是这样的:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.garden.navigationdrawer import NavigationDrawer
from kivy.properties import StringProperty
import paho.mqtt.client as mqtt

class Drawer(NavigationDrawer):
vartext1 = StringProperty("Button")
vartext2 = StringProperty("Text")

class MainMenuApp(App):
def build(self):
return Drawer()

def on_start(self):
topic = "kivy/#"

def onConnect(client, userdata, flags, rc):
mqttc.subscribe(topic, 0)

def onMessage(client, userdata, msg):
msg.payload = msg.payload.decode("utf-8")
print ("[INFO ] [MQTT ] topic: " + msg.topic +" msg: "+ msg.payload)
if msg.topic == "kivy/button":
userdata['self'].vartext1 = msg.payload

if msg.topic == "kivy/text":
userdata['self'].vartext2 = msg.payload

parameters = {'self': self}
mqttc = mqtt.Client(client_id="kivy-client", clean_session=True, userdata = parameters)
mqttc.on_connect = onConnect
mqttc.on_message = onMessage
mqttc.connect("10.0.0.104", 1883, keepalive=60, bind_address="")
mqttc.loop_start() # start loop to process callbacks! (new thread!)

if __name__ == "__main__":
MainMenuApp().run()

关于python - 将变量从 MQTT 传递给 Kivy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52465547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com