我搜索了几个小时,到底是什么原因导致此错误消息:我有一个搜索条目,它根据我使用回调函数的搜索更新列表框:
I searched for hours what the heck is the reason for this error message: I have a search entry, which update a listbox depending on my search with a callback function:
列表框:
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, pady=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, column=1, rowspan=3, padx=10, pady=1) self.lbox.config(yscrollcommand = self.scrollbar.set) self.scrollbar.grid(row=1, column=2, rowspan=3, padx=1, pady=1, sticky='ns') self.scrollbar.config(command=self.lbox.yview)所以如果我输入我的搜索,列表框会显示我的 sqlite 数据库中的一个简化的值列表,我很感兴趣.如果我用 dobble click 选择一个.另一个 sqlite 查询更新了我的组合框.
So If I type my search, the listbox show me a reduced list of values out of my sqlite database, I am interessed in. If I select one with dobble click. Another sqlite query update my comboboxes.
如果我选择一个,我会收到这个错误:
If I select one I get this error:
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 行来自另一个函数:
Line 337 comes from another function:
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知道吗,我可以搜索什么?
Any idea, what I can search for ?
推荐答案所以 self.e_fax 对我来说就像一个 ttk.Combobox.考虑这里的代码:
So self.e_faxseems like a ttk.Combobox to me. Consider this code here:
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()它通过相同的错误:
it throughs the same Error:
_tkinter.TclError: 索引 0 超出范围
_tkinter.TclError: Index 0 out of range
原因是列表 values 是空的,在其中插入任何常规字符串即可.如果要设置默认值,请确保有一个值.
and the reason is the list values is empty, insert any regular string in it and it works. Make sure if you want to set default value that there is an value.
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()更多推荐
tkinter 通过错误回调:“索引 0 超出范围";
发布评论