python 如何将串口数据写入Text widget
How to write serial port data to Text widget in python
我是 python 的初学者,并且在 tkinter 中遇到了一些问题。
下面是应用程序从我的蓝牙模块接收 GPS 数据的代码,我想将“数据”输出到我的文本小部件“outputtext”。
import serial
from tkinter import *
root = Tk()
serialPort = serial.Serial(port='COM8', baudrate=9600, timeout=0, parity=serial.PARITY_EVEN, stopbits=1)
size = 10240
def myClick():
while serialPort:
data = serialPort.readline(size)
if data:
print(data)
outputtext.insert('1.0', data)
outputtext = Text(root)
outputtext.pack()
myButton = Button(root, text="Retrieve Bluetooth Data!", command=myClick)
myButton.pack()
root.mainloop()
print 工作正常,但我无法将“数据”输出到“outputtext”。单击“myButton”后应用程序没有立即响应。这可能是由于我收到了大量数据(每秒 NMEA 句子)。也许我应该使用缓冲区或其他东西。任何帮助将不胜感激。
为此,我们需要一个线程。
更改以下行
myButton = Button(root, text="Retrieve Bluetooth Data!", command=myClick)
和
myButton = Button(root, text="Retrieve Bluetooth Data!", command=threading.Thread(target=myClick).start)
将允许您运行 myClick() 函数与其他函数并行
我是 python 的初学者,并且在 tkinter 中遇到了一些问题。 下面是应用程序从我的蓝牙模块接收 GPS 数据的代码,我想将“数据”输出到我的文本小部件“outputtext”。
import serial
from tkinter import *
root = Tk()
serialPort = serial.Serial(port='COM8', baudrate=9600, timeout=0, parity=serial.PARITY_EVEN, stopbits=1)
size = 10240
def myClick():
while serialPort:
data = serialPort.readline(size)
if data:
print(data)
outputtext.insert('1.0', data)
outputtext = Text(root)
outputtext.pack()
myButton = Button(root, text="Retrieve Bluetooth Data!", command=myClick)
myButton.pack()
root.mainloop()
print 工作正常,但我无法将“数据”输出到“outputtext”。单击“myButton”后应用程序没有立即响应。这可能是由于我收到了大量数据(每秒 NMEA 句子)。也许我应该使用缓冲区或其他东西。任何帮助将不胜感激。
为此,我们需要一个线程。 更改以下行
myButton = Button(root, text="Retrieve Bluetooth Data!", command=myClick)
和
myButton = Button(root, text="Retrieve Bluetooth Data!", command=threading.Thread(target=myClick).start)
将允许您运行 myClick() 函数与其他函数并行