Created
June 8, 2019 12:49
-
-
Save el3/e29ee32e46dc0a4ae496d8211039cf62 to your computer and use it in GitHub Desktop.
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 | |
def threaded(callback): | |
def wrapper(*args): | |
thread = threading.Thread(target=callback, args=args) | |
thread.start() | |
return thread | |
return wrapper | |
class MyBL(BoxLayout): | |
counter = 0 | |
data_label = StringProperty("Nothing yet!") | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
self.get_data() | |
@threaded | |
def get_data(self): | |
while App.get_running_app().running: | |
# 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): | |
running = True | |
def build(self): | |
return Builder.load_string(KV) | |
def on_stop(self): | |
self.running = False | |
MyApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment