wxPython TreeListCtrl显示神秘框

问题描述 投票:0回答:1

我是wxPython的新手。我无法正确显示wx.dataview.TreeListCtrl对象的第一列。具体来说,每个条目的第一列中的文本已被黑框替换。

Black boxes on the first column in wxPython's TreeListCtrl

这是我创建控件的方式:

tree_list_ctrl = wx.dataview.TreeListCtrl(self.component_tree_panel, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.dataview.TL_DEFAULT_STYLE)

for c in ["Group", "Name", "Id"]:
    tree_list_ctrl.AppendColumn(c, 80)

这是我要添加的数据:

root = tree_list_ctrl.GetRootItem()

for group_str in groups.keys():
    grp_node = tree_list_ctrl.AppendItem(root, "A GROUP")
    tree_list_ctrl.SetItemText(grp_node, 0, "A GROUP")
    tree_list_ctrl.SetItemText(grp_node, 1, "A")
    tree_list_ctrl.SetItemText(grp_node, 2, "B")

    for child_str in groups[group_str]:
        child_node = tree_list_ctrl.AppendItem(grp_node, "A CHILD")
        tree_list_ctrl.SetItemText(child_node, 0, "A CHILD")
        tree_list_ctrl.SetItemText(child_node, 1, "a")
        tree_list_ctrl.SetItemText(child_node, 2, "b")

我正在使用Python 3.7.4和wxPython 4.1.0。这只是带有许多控件的大型应用程序的一小部分-所有其他控件均按预期工作。只是这个TreeListCtrl的第一列使我感到悲伤。预先感谢。

python wxpython
1个回答
0
投票

那段代码没有错误。该错误必须在其他地方。在创建小部件的父级时肯定会出现问题。

请检查并运行:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import wx
import wx.dataview


class MainFrame(wx.Frame):
    groups = {
            "G1" : ["a", "b"]
        }

    def __init__(self, *args, **kwds):        
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((400, 300))
        self.tree_list_ctrl = wx.dataview.TreeListCtrl(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.dataview.TL_DEFAULT_STYLE)

        self.__set_properties()
        self.__do_layout()


    ''' 
    Setting widgets properties
    '''
    def __set_properties(self):

        self.SetTitle("frame")

        # Creating columns:        
        self.tree_list_ctrl.AppendColumn("Group", 80)
        self.tree_list_ctrl.AppendColumn("Name", 80)
        self.tree_list_ctrl.AppendColumn("Id", 80)

        # Populating:
        root = self.tree_list_ctrl.GetRootItem()
        for group_str in MainFrame.groups.keys():
            grp_node = self.tree_list_ctrl.AppendItem(root, "A GROUP")
            self.tree_list_ctrl.SetItemText(grp_node, 0, "A GROUP")
            self.tree_list_ctrl.SetItemText(grp_node, 1, "A")
            self.tree_list_ctrl.SetItemText(grp_node, 2, "B")

            for child_str in MainFrame.groups[group_str]:
                child_node = self.tree_list_ctrl.AppendItem(grp_node, "A CHILD")
                self.tree_list_ctrl.SetItemText(child_node, 0, "A CHILD")
                self.tree_list_ctrl.SetItemText(child_node, 1, "a")
                self.tree_list_ctrl.SetItemText(child_node, 2, "b")

    ''' 
    Setting layout properties
    '''        
    def __do_layout(self):        
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(self.tree_list_ctrl, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MainFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.