简体   繁体   中英

'str' object has no attribute 'get' attribute

How can I solve it...

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'

I already convert it in string or integer but not working

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)

I want to get a value that stores in val1 and search that value in the database

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM