【发布时间】:2020-07-02 05:06:05
【问题描述】:
我正在尝试使用 ttkwidgets 中的 CheckboxTreeview 将复选框放在我使用 SQLite 查询填充的树视图的每一行前面。这是我的代码:
from ttkwidgets import CheckboxTreeview
import tkinter as tk
import sqlite3
root = tk.Tk()
tree = CheckboxTreeview(root)
connection = sqlite3.connect('lambtracker_db.sqlite')
cursor = connection.cursor()
cmd = "select id_deathreasonid, death_reason from death_reason_table"
cursor.execute(cmd)
rows = cursor.fetchall()
for row in rows:
print(row)
tree.insert('', 'end', values=row)
tree.pack()
root.mainloop()
它会建立一个带有复选框的空白树。 empty checkbox treeview 我可以验证该命令是否正常工作,因为我确实在控制台中将其打印出来
(1, 'Died Unknown')
(2, 'Died Old Age')
(3, 'Died Predator')
(4, 'Died Illness')
(5, 'Died Injury')
(6, 'Died Stillborn')
(7, 'Died Birth Defect')
(8, 'Died Meat')
(9, 'Died Euthanized')
(10, 'Died Dystocia')
(11, 'Died Other')
(12, 'Died Culled')
但是标准的 Treeview 可以工作
import tkinter as tk
from tkinter import ttk
import sqlite3
root = tk.Tk()
tree = ttk.Treeview(root, column=("column1", "column2"), show='headings')
tree.heading("#1", text="id_deathreasonid")
tree.heading("#2", text="Death Reason")
connection = sqlite3.connect('lambtracker_db.sqlite')
cursor = connection.cursor()
cmd = "select id_deathreasonid, death_reason from death_reason_table"
cursor.execute(cmd)
rows = cursor.fetchall()
for row in rows:
print(row)
tree.insert('', 'end', values=row)
tree.pack()
root.mainloop()
standard treeview showing up properly 如果我尝试将列和标题信息添加到 CheckboxTreeview 版本,它看起来与标准 ttk.Treeview 版本相同
from ttkwidgets import CheckboxTreeview
import tkinter as tk
import sqlite3
root = tk.Tk()
tree = CheckboxTreeview(root, column=("column1", "column2"), show='headings')
tree.heading("#1", text="id_deathreasonid")
tree.heading("#2", text="Death Reason")
connection = sqlite3.connect('lambtracker_db.sqlite')
cursor = connection.cursor()
cmd = "select id_deathreasonid, death_reason from death_reason_table"
cursor.execute(cmd)
rows = cursor.fetchall()
for row in rows:
print(row)
tree.insert('', 'end', values=row)
tree.pack()
root.mainloop()
当我通过 SQLite 查询填充树视图时,如何在树视图中的行前面获得复选框?
【问题讨论】:
标签: python tkinter checkbox treeview