我使用Tkinter和sqlite3将前端连接到数据库应用程序的后端。
需要帮助查找导致此错误的潜在原因:
<div class="box">
<div class="header">
Header
</div>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi magnam iusto fugit illo omnis aperiam mollitia non fugiat, at in ratione harum ullam alias dicta, excepturi quod sed delectus veniam?<br><br> Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Nisi magnam iusto fugit illo omnis aperiam mollitia non fugiat, at in ratione harum ullam alias dicta, excepturi quod sed delectus veniam?<br><br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi magnam iusto fugit illo omnis aperiam
mollitia non fugiat, at in ratione harum ullam alias dicta, excepturi quod sed delectus veniam?<br><br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi magnam iusto fugit illo omnis aperiam mollitia non fugiat, at in ratione harum ullam
alias dicta, excepturi quod sed delectus veniam?<br><br>
</div>
<div class="footer">
Footer
</div>
</div>
但Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Peng\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "frontend.py", line 9, in get_selected_row
index=list1.curselection()[0]
IndexError: tuple index out of range
只是list1.curselection()[0]
,为什么它超出范围?
任何帮助找到出错的地方都将非常感谢!!!
我的代码:
frontend.py:
id
backend.py:
from tkinter import *
import backend
window=Tk()
list1.bind('<<ListboxSelect>>',get_selected_row)
def update_command():
backend.update(selected_tuple[0],title_text.get(),author_text.get(),year_text.get(),isbn_text.get())
def get_selected_row(event):
global selected_tuple
index=list1.curselection()[0]
selected_tuple=list1.get(index)
e1.delete(0,END)
e1.insert(END,selected_tuple[1])
e2.delete(0,END)
e2.insert(END,selected_tuple[2])
e3.delete(0,END)
e3.insert(END,selected_tuple[3])
e4.delete(0,END)
e4.insert(END,selected_tuple[4])
答案 0 :(得分:2)
当您在列表1中为空时单击它会发生 这是触发它的行
list1.bind('<<ListboxSelect>>',get_selected_row)
上面提到的解决方案很合适,因为您似乎没有任何其他代码可以操纵此事件 但是,为了与您的代码命名约定保持一致,我对解决方案的措辞有所不同:
def get_selected_row(event):
global selected_tuple
if list1.curselection():
index=list1.curselection()[0]
selected_tuple=list1.get(index)
e1.delete(0,END)
e1.insert(END,selected_tuple[1])
e2.delete(0,END)
e2.insert(END,selected_tuple[2])
e3.delete(0,END)
e3.insert(END,selected_tuple[3])
e4.delete(0,END)
e4.insert(END,selected_tuple[4])
答案 1 :(得分:1)
当没有选择任何内容时,将触发您的方法。最简单的解决方法是简单地检查元组是否为空:
def get_selected_row(event):
global selected_tuple
index=list1.curselection()
if index: # if the tuple is not empty
selected_tuple=list1.get(index[0])
e1.delete(0,END)
e1.insert(END,selected_tuple[1])
e2.delete(0,END)
e2.insert(END,selected_tuple[2])
e3.delete(0,END)
e3.insert(END,selected_tuple[3])
e4.delete(0,END)
e4.insert(END,selected_tuple[4])
更合适的解决方法是找出它被触发的时间并防止它。
答案 2 :(得分:0)
由于列表框为空,所以list1.curselection()将是一个没有项目的空列表。尝试使用第3行中的[0]访问该列表的第一项将引发错误,因为列表中没有第一项。
def get_selected_row(event):
try:
global selected_tuple
index=list1.curselection()
selected_tuple=list1.get(index[0])
e1.delete(0,END)
e1.insert(END,selected_tuple[1])
e2.delete(0,END)
e2.insert(END,selected_tuple[2])
e3.delete(0,END)
e3.insert(END,selected_tuple[3])
e4.delete(0,END)
e4.insert(END,selected_tuple[4])
except IndexError:
pass
调用get_selected_row函数时,Python将尝试在try下执行indentedblock。如果存在IndexErrorn,则将尝试执行以下行。而是将执行except下的那一行。通行证指的是什么也没做。因此,当列表框为空时,该功能将不执行任何操作。