我收到有关python TypeError的错误:' NoneType'对象没有属性' __ getitem __'

时间:2015-07-09 04:17:37

标签: python mysql uart

您好,我正在做我的学校项目,但我遇到了Python编程问题。 这是我的计划的来源。 它说

TypeError: 'NoneType' object has no attribute '__getitem__'
你觉得有什么问题?... 请帮我一把谢谢。

#!/usr/bin/env python

from Tkinter import *
import time
import serial
import MySQLdb

i = 3

ser = serial.Serial(
port='/dev/ttyAMA0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)

db = MySQLdb.connect("localhost", "root", "1234", "project")
curs = db.cursor()

def update_timeText():
    current = time.strftime("%H:%M:%S")
    timeText.configure(text=current)
    root.after(1000, update_timeText)

def update_db():
    global i
    x = ser.readline()
    if len(x) == 8:
        while x:
            if i == 20:
                i = 3
            Label(root, text= x, font=("Helvetica", 20), bg = 'white').grid(row=i,column=3)
            curs.execute("select name from matchdata where rfid = %s", (x))
            currentname = curs.fetchone()
            Label(root, text= currentname[0], font=("Helvetica", 20), bg = 'white').grid(row= i, column=0, columnspan=2)
            curs.execute("select sex from matchdata where rfid = %s", (x))
            currentsex = curs.fetchone()
            Label(root, text= currentsex, font=("Helvetica", 20), bg = 'white').grid(row=i, column=2)
            curs.execute("insert into readdata values(NOW(), %s)",(x))
            db.commit()
            i = i+1
            break;
    else:
        print "UART ERROR"
    root.after(10, update_db)

root = Tk()

root.configure(background='white')
logo = PhotoImage(file="logo.gif")
Label(root,image=logo).grid(row=0,column=0, rowspan = 1, columnspan = 4, sticky=W)

Label(root,text="< NAME >", font=("Helvetica",30), bg='white').grid(row= 2, column=0,columnspan=2)
Label(root,text="< SEX >", font=("Helvetica",30), bg='white').grid(row= 2, column=2, columnspan=1)
Label(root,text="< RFID >", font=("Helvetica",30), bg = 'white').grid(row= 2, column=3,columnspan=1)
timeText = Label(root,text="",font=("Helvetica",45),bg='white')
timeText.grid(row=0, column=3)
timeText.columnconfigure(0,weight=1)
timeText.rowconfigure(0,weight=1)
update_timeText()
update_db()
root.mainloop()

curs.close()
db.close()

2 个答案:

答案 0 :(得分:1)

模糊的问题 - 模糊的答案:Somwhere(确切地说是异常追踪告诉你的地方)你以这种方式访问​​一个对象:something[index]。也许是currentname[0]

在您的情况下,currentname(或其他任何内容)为None,因此无法使用[0]进行访问,您将获得所述异常。

答案 1 :(得分:1)

请注意curs.execute将元组作为其第二个参数。您的(x)仍评估为x,而不是包含x的元组。试试(x,),这是包含单个元素的元组的正确语法。