使列宽占用wxPython ListC中的可用空间

2024-05-14 00:47:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我的wx.ListCtrl(size=(-1,200))中有三列。我希望这些列在ListCtrl创建后填充它的宽度。理想情况下,第一列可以展开以填充可用的额外空间。第二列和第三列不需要展开,最好不会改变宽度(格式化ocd)。

目前,每个ListCtrl列都是使用(width=-1)设置的。

我觉得我可以利用这段代码来达到我的目的。。。

# Expand first column to fit longest entry item
list_ctrl.SetColumnWidth(0, wx.LIST_AUTOSIZE)

伪代码(可能):

# After wx.ListCtrl creation
Get width of ListCtrl control
Get width of each ListCtrl column
Calculate unused width of ListCtrl
Set first column width to original width + unused width

添加:

在下面的例子中,我不知道如何启动autowidthmixin。目前,我正试图将listctrl放入一个折叠面板中。foldpanel是一个类,类中的函数创建listctrl。我甚至不相信,鉴于目前我的代码结构,这是可以做到的!

class MyPanel(wx.Panel):

    def __init__(self, parent, dictionary):
        self.dictionary = dictionary
        """Constructor"""
        wx.Panel.__init__(self, parent)

        # Layout helpers (sizers) and content creation (setPanel)
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.mainSizer)
        list_ctrl = self.setPanel()
        self.mainSizer.Add(list_ctrl, 0, wx.ALL | wx.EXPAND, 5)
        self.GetSizer().SetSizeHints(self)

    def setPanel(self):
        index = 0

        list_ctrl = wx.ListCtrl(self, size=(-1, 200),
                                style=wx.LC_REPORT | wx.BORDER_SUNKEN)

        list_ctrl.InsertColumn(0, "Variable", format=wx.LIST_FORMAT_LEFT, width=-1)
        list_ctrl.InsertColumn(1, "x", format=wx.LIST_FORMAT_RIGHT, width=-1)
        list_ctrl.InsertColumn(2, u"\u03D0", format=wx.LIST_FORMAT_RIGHT, width=-1)

        for key, value in self.dictionary.iteritems():
            list_ctrl.InsertStringItem(index, str(key))
            list_ctrl.SetStringItem(index, 1, ("%.2f" % value[0]))
            list_ctrl.SetStringItem(index, 2, ("%.8f" % value[1]))
            index += 1

        list_ctrl.SetColumnWidth(0, wx.LIST_AUTOSIZE)
        list_ctrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        list_ctrl.SetColumnWidth(2, wx.LIST_AUTOSIZE)

        return list_ctrl

Tags: of代码selfindexdictionarycolumnwidthlist
1条回答
网友
1楼 · 发布于 2024-05-14 00:47:13

您需要使用listcrlautowidthmixin mixin类。wxPython演示应用程序在ListCtrl演示中有一个示例。根据documentation,可以使用它的setResizeColumn方法来告诉它要调整哪个列的大小。默认为最后一列。

编辑(07/05/2012):在代码中,创建一个类似于演示中的ListCtrl类。它看起来像这样:

    class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    def __init__(self, parent, ID, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
        listmix.ListCtrlAutoWidthMixin.__init__(self)
        self.setResizeColumn(0)

然后在实例化它时,只需调用list_ctrl=TestListCtrl(arg1,arg2…argN)

注意,我在上面的代码中包含了对setResizeColumn()的调用。它没有经过测试,但应该能起作用。

相关问题 更多 >