我的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中。 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
答案 0 :(得分:13)
您需要使用ListCtrlAutoWidthMixin mixin类。 wxPython演示应用程序在ListCtrl演示中有一个示例。根据{{3}},您可以使用其setResizeColumn方法告诉它要调整哪个列。默认值是最后一列。
EDIT(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()的调用。它没有经过测试,但应该可以使用。