You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Tkinter TreeView时插入的数据无法显示问题求助

TreeView无法显示已获取数据的排查与解决

我开发了一个程序,用于获取用户输入值并在表格中展示对应数据,但无论数据已被成功获取并打印,TreeView组件的列中始终无法更新和显示数据,已查阅相关资料但未找到解决方案。

相关代码

def show_data(self,panel_value):
    try:
        for item in self.get_children():
            self.delete(item)
        Current_data = db_file("database.db").get_data(panel_value)
        print(Current_data)
        self.insert_data(DB_data=Current_data)
        self.update()
    except Exception as e:
        print(f"{e}")
        
def insert_data(self,DB_data):
    for row in DB_data:
        self.insert("", "end", values=row)
        print("Row inserted:", row)

数据库结构

("Floor_Level" text, "Panel_name" text, "Demand_load" float, "Load_Current" float, "Safety_Factor" float, "CB_Type" text, "CB_Rating" integer, "No_of_cables" integer, "No_of_Cores" text, "Derating_Factor" float, "Incoming_Cable" integer, "Cable type" text, "ECC" text, "Length" float, "VD_PER" float, "Acc_VD" float, "SC_PER" float )

数据样本

sample1= [('GROUND FLOOR', 'SMDB-1', 65.0, 93.0, 1.0, 'MCCB', 100, 1, '4C', 0.7, 35, 'CU/PVC', '1X16', 120.0, 3.0, 3.0, 3.0), ('FIRST FLOOR', 'SMDB-2', 148.0, 213.0, 1.0, 'MCB', 63, 1, '4C', 0.7, 150, 'CU/PVC', '1X95', 90.0, 2.0, 2.0, 2.0), ('SECOND FLOOR ', 'SMDB-3', 161.0, 232.0, 2.0, 'ACB', 250, 1, '4C', 0.7, 150, 'CU/PVC', '1X95', 85.0, 2.0, 2.0, 2.0)]

sample2= [('sample 2 GROUND FLOOR', 'SMDB-1', 65.0, 93.0, 1.0, 'MCCB', 100, 1, '4C', 0.7, 35, 'CU/PVC', '1X16', 120.0, 3.0, 3.0, 3.0), ('FIRST FLOOR', 'SMDB-2', 148.0, 213.0, 1.0, 'MCB', 63, 1, '4C', 0.7, 150, 'CU/PVC', '1X95', 90.0, 2.0, 2.0, 2.0), ('SECOND FLOOR ', 'SMDB-3', 161.0, 232.0, 2.0, 'ACB', 250, 1, '4C', 0.7, 150, 'CU/PVC', '1X95', 85.0, 2.0, 2.0, 2.0)]

排查方向与解决方法

1. 确认TreeView列配置匹配

TreeView必须提前定义与数据字段数量、顺序完全一致的列,否则数据无法映射到列中。检查初始化代码,确保:

  • columns参数包含数据库返回的所有17个字段,顺序一致
  • 隐藏了默认空白列(设置show='headings')
  • 为每个列设置了表头和显示宽度

示例初始化代码:

import tkinter as tk
from tkinter import ttk

class MyTreeView(ttk.TreeView):
    def __init__(self, parent):
        super().__init__(parent)
        # 定义与数据库字段顺序一致的17个列
        self['columns'] = ("Floor_Level", "Panel_name", "Demand_load", "Load_Current", 
                           "Safety_Factor", "CB_Type", "CB_Rating", "No_of_cables", 
                           "No_of_Cores", "Derating_Factor", "Incoming_Cable", 
                           "Cable type", "ECC", "Length", "VD_PER", "Acc_VD", "SC_PER")
        self['show'] = 'headings'
        # 设置表头和列宽
        for col in self['columns']:
            self.heading(col, text=col)
            self.column(col, width=100, anchor='center')

2. 确保self指向TreeView实例

如果show_data和insert_data所在的类不是TreeView子类,self可能指向主窗口或其他容器,而非TreeView组件。此时需要直接调用TreeView实例的方法:

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.tree = MyTreeView(self)
        self.tree.pack()
    
    def show_data(self, panel_value):
        try:
            # 清空旧数据时调用TreeView实例的方法
            for item in self.tree.get_children():
                self.tree.delete(item)
            Current_data = db_file("database.db").get_data(panel_value)
            print(Current_data)
            self.insert_data(DB_data=Current_data)
            self.tree.update_idletasks()
        except Exception as e:
            print(f"{e}")
            
    def insert_data(self, DB_data):
        for row in DB_data:
            # 调用TreeView实例的insert方法
            self.tree.insert("", "end", values=row)
            print("Row inserted:", row)

3. 替换UI刷新方法

self.update()可能无法触发即时刷新,改用self.update_idletasks()强制处理待更新的UI任务:

# 替换show_data中的self.update()
self.update_idletasks()

4. 验证数据格式

额外确认:

  • Current_data的每个元素都是长度为17的元组(与列数严格匹配)
  • 数据中没有None或TreeView无法识别的自定义类型

内容的提问来源于stack exchange,提问作者Mohab Moha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 10:34:56