Python 3.x Tkinter串行读取

时间:2014-11-24 06:18:23

标签: python python-3.x tkinter serial-port

我无法通过串行连接读取来自arduino的串行数据。为了解决串口问题和需要同时运行的gui问题,我使用.after函数每100ms调用一次更新序列。但是,当我运行此代码时,我没有弹出窗口,并且我收到一条错误消息,说我已超出最大递归深度。这是我的代码:

'''
Created on Nov 23, 2014

@author: Charlie
'''

if __name__ == '__main__':
    pass

import serial
from tkinter import *

ser = serial.Serial('COM8')
ser.baudrate = 9600

def update():
    c = StringVar()
    c=ser.readline()
    theta.set(c)
    root.after(100,update())

root=Tk()
theta = StringVar()

w = Label(root, textvariable = theta)
w.pack()

root.after(100,update())    
root.mainloop()

2 个答案:

答案 0 :(得分:0)

您应该使用root.after(100, update)。注意update之后缺少括号。使用括号将update的结果发送到after调用,但要计算结果,必须运行update,其中包含对需要结果的after的另一个调用update等等..

另见this question

另外,为什么每次调用StringVar函数时都会创建一个新的updatec = ser.readline()无论如何都会覆盖c,所以您也可以删除该行。

答案 1 :(得分:-1)

在func update()中删除周期root.after(100,update())。这样:

def update():
    c = StringVar()
    c=ser.readline()
    theta.set(c)
相关问题