如何解决tkinter 通过错误回调:“索引 0 超出范围”
我搜索了几个小时,到底是什么原因导致此错误消息: 我有一个搜索条目,它根据我使用回调函数的搜索更新列表框:
列表框:
self.name_search=tk.StringVar()
self.name_search.trace_add('write',self.my_callback)
self.e_name_search_text = tk.Label(search_f,text="Name: ").grid(row=0,column=0,padx=10,pady=5,sticky='E')
self.e_name_search = ttk.Entry(search_f,width = 35,textvariable=self.name_search)
self.e_name_search.grid(row=0,column=1,padx=5,sticky='W')
self.lbox = tk.Listbox(search_f,width=35,height=8)
self.lbox.bind("<Double-Button-1>",self.show_name_search)
self.lbox.bind('<Return>',self.show_name_search)
self.scrollbar = tk.Scrollbar(search_f)
self.lbox.grid(row=1,rowspan=3,pady=1)
self.lbox.config(yscrollcommand = self.scrollbar.set)
self.scrollbar.grid(row=1,column=2,padx=1,pady=1,sticky='ns')
self.scrollbar.config(command=self.lbox.yview)
所以如果我输入我的搜索,列表框会显示我的 sqlite 数据库中的一个简化的值列表,我很感兴趣。如果我用 dobble click 选择一个。另一个 sqlite 查询更新了我的组合框。
如果我选择一个,我会收到这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python38-32\lib\tkinter\__init__.py",line 1883,in __call__
return self.func(*args)
File "D:\... name.py",line 337,in show_name_search
self.e_fax.current(0)
File "C:\Python38-32\lib\tkinter\ttk.py",line 717,in current
return self.tk.call(self._w,"current",newindex)
_tkinter.TclError: Index 0 out of range
第 337 行来自另一个函数:
def show_name_search(self,event):
self.clear_field()
widget = event.widget
selection = widget.curselection()
indName = widget.get(selection[0])
print(indName)
print("selktierter Wert: {}".format(indName))
self.realName.set(indName)
connection = sqlite3.connect(select_connect_db)
print('Database connected.')
with connection:
cursor = connection.cursor()
cursor.execute("SELECT number,type,prio,id,uniqueid FROM numbers WHERE realName=?;",(indName,))
data = cursor.fetchall()
print(data)
for row in data:
if row[1] == 'home':
self.phone_home.append(row[0])
print('HOME:',self.phone_home)
if row[1] == 'mobile':
self.mobile.append(row[0])
print('Mobile:',self.mobile)
if row[1] == 'work':
self.business.append(row[0])
print(row[0])
print('WORK:',self.business)
if row[1] == 'fax_work':
self.fax.append(row[0])
print(row[0])
print('FAX_WORK:',self.fax)
self.uid_name.set(row[4])
if len(self.phone_home) != 0:
self.e_phone['values'] = self.phone_home
self.e_phone.current(0)
if len(self.mobile) != 0:
self.e_mobile['values'] = self.mobile
self.e_mobile.current(0)
if len(self.business) != 0:
self.e_business['values'] = self.business # Set the value to the new list
self.e_business.current(0) # Set the first item of the list as current item
if len(self.business) != 0:
self.e_fax['values'] = self.fax
self.e_fax.current(0) ### Line 337 - No entry for this value in my sqlite database
知道吗,我可以搜索什么?
解决方法
所以 self.e_fax
对我来说似乎是一个 ttk.Combobox。考虑这里的代码:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
values = []
lb = ttk.Combobox(root,values=values)
lb.current(0)
lb.pack()
root.mainloop()
它通过相同的错误:
_tkinter.TclError: 索引 0 超出范围
原因是列表 values
是空的,在其中插入任何常规字符串即可。
确保你想设置默认值,有一个值。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
values = ['see']
lb = ttk.Combobox(root,values=values)
lb.current(0)
lb.pack()
root.mainloop()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。