python - “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
数据库中的值并在数据库中搜索该值
解决方案
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."
推荐阅读
- javascript - 如何在数据表中添加禁用列
- server - ejabberd 19.09 安装在 Windows 10 上,管理页面给出 404 页面未找到
- javascript - 如何使标题元素的宽度缓慢向左移动
- swift - UICollectionViewCell 阴影和圆角不起作用
- powerapps - 从 PowerApps 创建页面时的 OneNote 嵌套列表问题
- flutter - 检查表单条目是否为 10 的倍数
- graphics - 未解析的外部符号“类 Fl_Graphics_Driver * fl_graphics_driver”
- javascript - 如何在javascript中将文本输入变量添加到URL
- css - 我应该如何在我的项目中构建 sass 和 css
- python - 如何使用物理按钮在热敏打印机上打印内容?