我该如何在这段代码中使用while循环每隔2秒读取串口数据并显示在标签中?由于对Python不熟悉,这个应用程序在运行时可能会挂起。
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from time import sleep
import serial
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 2
self.rows = 2
ser = serial.Serial('COM3', 9600, timeout=0)
while 1:
sleep(2)
ser.read()
data = ser.read()
self.add_widget(Label(text=str(data)))
class MyApp(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
MyApp().run()