Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

There's a project I am working on and I am using kivy. I receive some input and that input gets stored in a variable that changes almost every 50 milliseconds I just want kivy to print JUST the current value of the variable in the text box. I know this is not a good representation of the question. But I just tried to simplify it.

If you didn't understand the question very well, the next section is a bit more detailed:

I am using an arduino to get some input. That input is stored into a variable myData. So obviously it keeps changing. I want myData to be printed in the kivy screen. You don't really need knowledge of arduino for this question. Heres the python code for arduino:

import serial
import time



info = serial.Serial('com3',9600)

def arduino():
        while True:
                if info.inWaiting()>0 :
                        myData = info.readline()
                        myData = myData.decode('utf-8')
                        
        

arduino()
       

Python code for kivy is pretty standard. The kivy file is what I need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
171 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just make a label, schedule a Clock update, and return it...

from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock

import serial
import time

myData = ""
info = serial.Serial('com3',9600)


def arduino():
    global myData
    while True:
        if info.inWaiting() > 0 :
            myData = info.readline()
            myData = myData.decode('utf-8')

arduino()

class MyLabel(Label):
    
    def update_text(self, dt):
        global myData
        self.text = myData    # Update text

    def on_kv_post(self, instance):
        Clock.schedule_interval(self.update_text, 1/60)    # Scheduling


class MyApp(App):
    def build(self):
        label = MyLabel()
        return label


MyApp().run()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...