2015-02-06 98 views
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 

能有人幫助我 感謝

+4

這意味着您的字符串小於2個字符。你可能會得到一個空字符串,只需用'print'或/和'len'來檢查 – nbro 2015-02-06 02:40:57

回答

0

事實上,我的字符串比2個字符小。所以我只是檢查了它的長度並修改了我的條件,謝謝,我並沒有一直獲得2或3個元素,有時候只有1個或沒有任何元素

相關問題