我有一個數據庫與客戶表與字段(id,名稱,姓氏,出生年份)。 我創建了一個顯示名字和姓氏的樹形視圖,但是當我嘗試將DOB插入到樹形視圖中時,出現錯誤列索引3越界。 也請你能解釋一下treeview是如何實際工作的,因爲我已經從教程中複製了代碼並對其進行了修改,但仍未完全理解它。Python - tkinter - 樹 - 數據庫問題
from tkinter import *
from tkinter import ttk
import sqlite3
import os.path
class Product:
db_name = 'database.db'
def __init__(self, wind):
self.wind = wind
self.wind.title('IT Products')
frame = LabelFrame (self.wind, text = 'Add new record')
frame.grid (row = 0, column = 1)
Label (frame, text = 'Name: ').grid (row = 1, column = 1)
self.name = Entry (frame)
self.name.grid(row = 1, column = 2)
Label (frame, text = 'Surname: ').grid (row = 2, column = 1)
self.price = Entry (frame)
self.price.grid(row = 2, column = 2)
ttk.Button (frame, text= 'Add record', command = self.adding).grid (row = 3, column =2)
self.message = Label (text = '',fg = 'red')
self.message.grid (row = 3, column = 0)
self.tree = ttk.Treeview (height = 10, colum = 2)
self.tree.grid(row = 100, column = 0, columnspan = 100)
self.tree.heading('#0', text = 'Name', anchor = W)
self.tree.heading(2, text = 'Surname', anchor = W)
self.tree.heading(3, text = 'DOB', anchor = W)
self.tree.heading(3, text = "column B")
ttk.Button (text = 'Delete record', command = self.deleting).grid (row = 10, column = 5)
ttk.Button (text = 'Edit record').grid (row = 10, column = 1)
self.viewing_records()
def run_query (self, query, parameters =()):
with sqlite3.connect(self.db_name) as conn:
cursor = conn.cursor()
query_result = cursor.execute (query, parameters)
conn.commit()
return query_result
def viewing_records(self):
records = self.tree.get_children()
for element in records:
self.tree.delete (element)
query = 'SELECT * FROM customers '
db_rows = self.run_query (query)
for row in db_rows:
## self.tree.insert('', 2, text=str(), values=(row[1], row[2],
row[3]))
self.tree.insert ('', 2, text = row[1], values=(row[1], row[2], row[3]))
def validation(self):
return len (self.name.get()) != 0 and len (self.price.get()) != 0
def adding (self):
if self.validation():
query = 'INSERT INTO customersVALUES (NULL, ?, ?)'
parameters = (self.name.get(), self.price.get())
self.run_query (query, parameters)
self.message['text'] = 'Record {} added'.format (self.name.get())
self.name.delete (0, END)
self.price.delete (0,END)
else:
self.message['text'] = 'Name field or price is empty'
self.viewing_records()
def deleting (self):
self.message ['text'] = ''
try:
self.tree.item(self.tree.selection()) [ 'values'] [0]
except IndexError as e:
self.message[text] = 'Please select record'
return
self. message['text'] = ''
name = self.tree.item(self.tree.selection())['text']
query = 'DELETE FROM customers WHERE name = ?'
self.run_query(query, (name,))
self.message['text'] = 'Record {} deleted'.format(name)
self.viewing_records()
if __name__ == '__main__':
wind = Tk()
application = Product (wind)
wind.mainloop()
拼錯 「塔」,self.tree = ttk.Treeview(高度= 10,colum = 2),並且指定了具有2列的樹視圖,因此錯誤消息是正確的。最後,我們不知道「但是還沒有完全理解」是什麼意思。你想要弄清楚什麼發生了什麼? –