'str'对象没有属性'get'属性

时间:2019-01-25 10:11:46

标签: python python-3.x

我该如何解决...

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Mani\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "F:\Monu\Work\python\PROJECT\New folder\LMS.py", line 262, in onDoubalclick
    cursor.execute("SELECT * FROM `TRANSECTION` WHERE Book_Id=?",(val1.get(),))
AttributeError: 'str' object has no attribute 'get'

我已经将其转换为字符串或整数,但无法正常工作

def onDoubalclick(event):
    test=treeview.item(treeview.selection())
    print(test)
    items=treeview.selection()[0]
    val1=str(treeview.item(items)['values'][0])
    print(type(val1))    
    popsearch()
    DataBase()
    cursor.execute("SELECT * FROM `TRANSECTION` WHERE Book_Id=?",(val1.get(),))
    info=cursor.fetchall()
    for ROW1 in info:
        print(rows)
        treeview2.insert("",END,value=ROW1)

我想获取一个存储在val1中的值,并在数据库中搜索该值

1 个答案:

答案 0 :(得分:0)

错误消息是正确的字符串没有get属性。

这是防止此错误导致程序崩溃的最简单方法。我刚刚从get()变量中删除了val1函数/方法调用。

def onDoubalclick(event):
    test=treeview.item(treeview.selection())
    print(test)
    items=treeview.selection()[0]
    val1=str(treeview.item(items)['values'][0])
    print(type(val1))    
    popsearch()
    DataBase()
    cursor.execute("SELECT * FROM `TRANSECTION` WHERE Book_Id=?",(val1,))
    info=cursor.fetchall()
    for ROW1 in info:
        print(rows)
        treeview2.insert("",END,value=ROW1)

另一个选择是不修正错误,而是将错误包围在try / except块中,以防止程序崩溃。

例如,您可以执行以下操作:

#more code above left off to keep things simple

try:
    cursor.execute("SELECT * FROM `TRANSECTION` WHERE Book_Id=?", (val1.get(),))
    info=cursor.fetchall()
    #the rest of your code

except Exception as e:
    print "This exception was thrown: %s" % str(e)

#the rest of your code
print "Blah. Print stuff here. Print variable state. Print time."