Skip to content

Instantly share code, notes, and snippets.

@el3
Last active March 6, 2025 21:17
Show Gist options
  • Save el3/3c8d4e127d41e86ca3f2eae94c25c15f to your computer and use it in GitHub Desktop.
Save el3/3c8d4e127d41e86ca3f2eae94c25c15f to your computer and use it in GitHub Desktop.
threading in kivy example
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.clock import mainthread
import threading
import time
class MyBL(BoxLayout):
counter = 0
data_label = StringProperty("Nothing yet!")
def __init__(self, **kwargs):
super().__init__(**kwargs)
threading.Thread(target=self.get_data).start()
def get_data(self):
while App.get_running_app():
# get data here
# sock.recv(1024) or how you do it
time.sleep(1)
# if you change the UI you need to do it on main thread
self.set_data_label(self.counter)
self.counter += 1
@mainthread
def set_data_label(self, data):
self.data_label = str(data)
KV = """
MyBL:
Label:
font_size: "30sp"
text: "Some Data"
Label:
font_size: "30sp"
text: root.data_label
"""
class MyApp(App):
def build(self):
return Builder.load_string(KV)
MyApp().run()
@joergBeigang
Copy link

Would have never figured that by my own. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment