0
我试图更改标签的颜色,一旦我的串行端口识别** @ *或**`*IndexError:字符串索引超出范围,pyserial的Tkinter,蟒蛇3.4
这是我的代码
while set_ser.isOpen():
time.sleep(0.0001)
bytess = set_ser.inWaiting()
bytes = set_ser.read(bytess)
get_serial = str(bytes.decode("utf-8", errors="ignore"))
loop.set(get_serial)
if (get_serial[2]=='@'): #busy
sLabelRead.configure(bg = 'red', fg='white')
sLabelRead.place(x=10,y=600)
mGui.update()
elif (get_serial[2]=='`'): #ready:
sLabelRead.configure(bg = 'orange', fg='black')
sLabelRead.place(x=10,y=600)
mGui.update()
elif (get_serial==''): #none:
sLabelRead.configure(bg = 'black', fg='white')
sLabelRead.place(x=10,y=600)
mGui.update()
mGui.update()
else:
print (get_serial)
mGui.update()
但我得到这个错误
if (get_serial[1]=='@'): #busy
IndexError: string index out of range
能有人帮助我 感谢
这意味着您的字符串小于2个字符。你可能会得到一个空字符串,只需用'print'或/和'len'来检查 – nbro 2015-02-06 02:40:57