我将创建一个非常简单的Kivy应用程序来读取串口并在标签中显示它,在Python控制台中,我可以看到串行数据,但在Pycharm和Kivy中结果是错误的。
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
import serial
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
ser = serial.Serial('COM3', 9600, timeout=0)
ser.read()
super(LoginScreen, self).__init__(**kwargs)
self.cols = 2
self.rows = 2
self.add_widget(Label(text=str(ser)))
class MyApp(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
MyApp().run()