#:kivy 2.0#:import kivy kivy
#:import win kivy.core.window
FloatLayout:
Image:
size_hint: None, None
size: 220,220
pos_hint: { 'center_x': 0.4, 'center_y': 0.5 }
allow_stretch: True
source: app.weather_icon
Label:
pos_hint: { 'center_x': 0.8, 'center_y': 0.6 }
#bold: True
color: 1, 0.3, 0.3, 1
font_size: 72
text: '%d'%app.temp_max
Label:
color: 0.7, 0.7, 1, 1
pos_hint: { 'center_x': 0.8, 'center_y': 0.3 }
#bold: True
font_size:56
text: '%d'%app.temp_min
Button:
text: 'Hit me'
on_press: app.kvcallback()
size_hint_x: None
height: 50
import paho.mqtt.client as mqtt
import kivy
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import StringProperty, NumericProperty
class TouchApp(App):
weather_icon=StringProperty('1.png')
temp_max=NumericProperty(0.0)
temp_min=NumericProperty(0.0)
doit=False
client=None
def build(self):
self.temp_max=5.0
self.temp_min=-4.0
self._mqttConnect()
return
def _mqttConnect( self ):
self.client=mqtt.Client( client_id='kivytest', userdata=None, protocol=mqtt.MQTTv311, transport="tcp")
self.client.on_message=self.mqttcallback
self.client.connect( '172.30.10.3', port=8883, keepalive=60 )
self.client.subscribe ('#', 0)
self.client.loop_start()
return
def mqttcallback(self, client, userdata, message):
if self.doit:
self.temp_max=self.temp_max+1
self.weather_icon='3.jpg'
else:
# skip the first update
self.doit=True
return
def kvcallback(self):
print("kv callback called")
self.weather_icon='4.jpg'
return
if __name__ == '__main__':
TouchApp().run()
I am assuming you are having an issue because you have 2 event loop based systems “competing” with each other.
Kivy operates on an event loop. Calling app.run() starts the event loop. It looks like paho mqtt is also event loop based, this is how it is managing the callbacks. I suspect what is happening is that the code is sitting in the mqtt event loop – not the kivy event loop, so the kivy event never fires.
I took a quick look at the MQTT docs, and saw this section: https://www.eclipse.org/paho/index.php?page=clients/python/docs/index.php#external-event-loop-support
I expect you could use these calls to integrate MQTT into the kivy event loop. You can use the Clock.schedule_interval() to schedule these calls. See: https://kivy.org/doc/master/api-kivy.clock.html?highlight=clock#module-kivy.clock
FYI: The convention in python is to only use a return statement at the end of a method if you are returning a value.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAJoVLP7bmLVbhyMLRzp9HM1D0edSVK%3DTPG3gGt2MYuAxXP7fCQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61ace587.1c69fb81.92c2c.3cd1SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
def mqttcallback(self, mqttclient, userdata, message):
Clock.schedule_once(partial(self.clock_mqtt_callback, mqttclient, userdata, message ))
def clock_mqtt_callback(self, mqttclient, userdata, message, dt):
self.temp_max=self.temp_max+1
self.weather_icon='3.jpg'
I’m delighted to hear you got things working. I would caution you that this seems like a rather fragile solution. You are at high risk of dropping messages because your MQTT loop is not running – or locking up the GUI because you are not running the Kivy event loop.
I’d recommend you use the clock interval calls. These will simple establish a schedule to poll the items in the Kivy event loop that would normally be polled in the MQTT event loop.
Of course this is your code – so it is your call. If it is working…
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAJoVLP6FBttPC0nE87hfp42mFKhk%3Dd5i6vOTbw-zRKx9wJGQ8Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61b12011.1c69fb81.f413d.000eSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
😎 Thanks for the follow up!
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAJoVLP4JuTPvn8q_UQzKzHw_DhuYH-zSY7PwjgXB1L1%3D7yo-sQ%40mail.gmail.com.