【问题标题】:Make column width take up available space in wxPython ListCtrl使列宽占用 wxPython ListCtrl 中的可用空间
【发布时间】:2012-07-04 02:05:37
【问题描述】:

我的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

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    您需要使用 ListCtrlAutoWidthMixin 混合类。 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() 的调用。它未经测试,但应该可以工作。

    【讨论】:

    • 添加了一些额外的信息和代码sn-p。鉴于我的代码 atm 的布局,我无法弄清楚如何使用 mixin 类。
    猜你喜欢
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 2014-10-26
    • 2011-09-25
    相关资源
    最近更新 更多