如何在树视图Tkinter Python中获取第一个标题

92dk7w1h  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(151)

我写了下面的代码:

def fee_foo():
    fee_screen = tk.Tk()
    fee_screen.title("Fee Information")
    fee_screen.geometry("500x500")
  # Execute the SELECT statement and retrieve the results
    # Execute the SELECT statement and retrieve the results
    query = "SELECT roll_no, paid, due_date, amount FROM fee"
    cursor.execute(query)
    results = cursor.fetchall()

    # Create the Treeview widget
    treeview = ttk.Treeview(fee_screen)
    treeview.pack(fill=tk.BOTH, expand=True)

    # Configure the columns of the Treeview widget
    treeview["columns"] = ("roll_no","paid", "due_date", "amount")
    treeview.column("roll_no",width=100)
    treeview.column("paid", width=100)
    treeview.column("due_date", width=100)
    treeview.column("amount", width=100)

    # Set the column headers of the Treeview widget
    treeview.heading("roll_no",text="Roll No")
    treeview.heading("paid", text="Paid")
    treeview.heading("due_date", text="Due Date")
    treeview.heading("amount", text="Amount")
    # Insert the rows into the Treeview widget
    for result in results:
        treeview.insert("", tk.END, text=result[0], values=result[1:])

但我得到的结果是这样的:enter image description here您能告诉我代码有什么问题吗?
我尝试更改列和标题变量的值,并在treeview.insert行中重新索引元组。

lb3vh1jj

lb3vh1jj1#

如果我们这样修改列的排列,我找到了答案:

# Configure the columns of the Treeview widget
    treeview["columns"] = ("paid", "due_date", "amount")
    treeview.column("paid", width=100)
    treeview.column("due_date", width=100)
    treeview.column("amount", width=100)

    # Set the column headers of the Treeview widget
    treeview.heading("#0", text="Roll No")
    treeview.heading("paid", text="Paid")
    treeview.heading("due_date", text="Due Date")
    treeview.heading("amount", text="Amount")

    # Insert the rows into the Treeview widget
    for result in results:
        treeview.insert("", tk.END, text=result[0], values=result[1:])

它显示正确的列排列

相关问题