首页 > 解决方案 > “str”对象没有属性“get”属性

问题描述

我该如何解决它...

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数据库中的值并在数据库中搜索该值

标签: pythonpython-3.x

解决方案


The error message is correct Strings do not have a get attribute.

This is the easiest way to prevent this error from crashing your program. I just removed the get() function/method call from the val1 variable.

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)

Another option is to not fix the error but surround the bug in a try/except block to prevent the program from crashing.

So as an example you could do the following:

#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."

推荐阅读