Skip to content

Instantly share code, notes, and snippets.

@el3
Last active April 16, 2024 22:01
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()
@Jray-Tech
Copy link

This helped me a lot! Thank you. I have been trying to solve this bug for about 12hours now

@cederom
Copy link

cederom commented Nov 26, 2022

TANK U SIR! :-)

@dovczitter
Copy link

Thanks, this solved implementing email in kivy/android.
Keep up the good work.

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