Last active
March 6, 2025 21:17
-
-
Save el3/3c8d4e127d41e86ca3f2eae94c25c15f to your computer and use it in GitHub Desktop.
threading in kivy example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
TANK U SIR! :-)
Thanks, this solved implementing email in kivy/android.
Keep up the good work.
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
This helped me a lot! Thank you. I have been trying to solve this bug for about 12hours now