【发布时间】:2020-08-12 16:11:04
【问题描述】:
我无法解决 Tkinter 数据库 (sqlite3) 中的搜索查询问题。我的部分代码:
front.py
# Entries self.name_text = tk.StringVar() self.entry_name = tk.Entry(self.parent, textvariable=self.name_text) self.entry_name.grid(row=3, column=1) self.color_text = tk.StringVar() self.combobox2=ttk.Combobox(self.parent, textvariable=self.color_text) self.combobox2["values"] = ('red','blue','white') self.labelCombobox=ttk.Label(self.parent, textvariable=self.color_text) self.combobox2.grid(row=4, column=1) self.parent.bind('<Return>',lambda e:refresh()) def search_command(self): self.listBox.delete(0,tk.END) for row in backend.database.search(self.name_text.get(),self.color_text.get()): self.listBox.insert(tk.END, row)backend.py 类数据库:
def search(name="",color=""): try: connect = sqlite3.connect("color.db") cur = connect.cursor() sql = "SELECT * FROM color WHERE name=? OR color=?" values = (self, name_text.get(), color_text.get()) cur.execute(sql, values) rows = cur.fetchall() name_text.set(rows[1]) color_text.set(rows[2]) entry_name.configure('disabled') combobox2.configure('disabled') connect.close() except: messagebox.showinfo('nothing found!')
我还尝试将 self 放入其他版本的 backend.py 中。这给出了同样的错误。
def search(self, name="",color=""): try: self.connect = sqlite3.connect("color.db") self.cur = self.connect.cursor() self.sql = "SELECT * FROM color WHERE name=? OR color=?" self.values = (self, name_text.get(), color_text.get()) self.cur.execute(sql, values) self.rows = self.cur.fetchall() self.name_text.set(rows[1]) self.color_text.set(rows[2]) self.entry_name.configure('disabled') self.combobox2.configure('disabled') self.connect.close() except: messagebox.showinfo('nothing!')
请帮忙解决错误:
对于 backend.database.search(self.name_text.get(),self.color_text.get()) 中的行: TypeError: 'NoneType' 对象不可迭代
【问题讨论】:
-
该错误告诉您搜索返回
None。您的代码需要处理这种情况。另外,您为什么认为需要添加self作为要查询的值之一? -
我知道这意味着它返回 None (无行)。但不知道如何重写代码。自我没有工作。
-
您正在使用
search方法的结果,但您似乎没有从search方法返回值。您发布的代码是该功能的完整代码吗?