0

我一直在使用 wxPython 创建一个应用程序。我想使用 ListCtrl 显示一些数据,并且我希望列占用最大空间。这是:可用的最大大小在我拥有的行之间划分。我已经尝试过了,但它不起作用(这是 listctrl 所在的面板):

class VirtualListPanel(wx.Panel):
   def __init__(self, parent):
    wx.Panel.__init__(self, parent, -1)

    width = self.GetParent().GetClientSize().width/5
    coches = vehiculos()    
    gris = wx.Color(220,220,220)

    self.lista = wx.ListCtrl(self, style=wx.LC_REPORT|wx.LC_HRULES|wx.LC_VRULES)
    self.lista.InsertColumn(0, "Matricula", width=width)
    self.lista.InsertColumn(1, "Tipo", width=width)
    self.lista.InsertColumn(2, "Matriculacion", width=width)
    self.lista.InsertColumn(3, "Ult. ITV", width=width)
    self.lista.InsertColumn(4, "Prox. ITV", width=width)

    i = 0   
    for data in coches:
        index = self.lista.InsertStringItem(i, data[0])
            self.lista.SetStringItem(index, 1, str(data[1]))
            self.lista.SetStringItem(index, 2, str(data[2]))
        self.lista.SetStringItem(index, 3, str(data[3]))
        self.lista.SetStringItem(index, 4, str(prox(data[1],data[2],data[3])))
        if((index+1) % 2 == 0):
            self.lista.SetItemBackgroundColour(index,gris)
        i += 1

    self.sizer = wx.BoxSizer(wx.HORIZONTAL)
    self.sizer.Add(self.lista, 1, wx.EXPAND|wx.ALL)
    self.SetSizer(self.sizer)

    self.Bind(wx.EVT_SIZE, self.OnResize)

def OnResize(self, event):
        width = self.GetParent().GetClientSize().width/5
    self.lista.SetColumnWidth(0, width)
    self.lista.SetColumnWidth(1, width)
    self.lista.SetColumnWidth(2, width)
    self.lista.SetColumnWidth(3, width)
    self.lista.SetColumnWidth(4, width)

现在,它甚至没有占据整个窗口,它只在左上角显示一个小方块。如果我评论“self.Bind(wx.EVT_SIZE ...”行,它会显示全屏,但列很小。

先感谢您!!!

4

2 回答 2

1

您缺少的是函数event.Skip()中的调用OnResize。可以在此处找到有关事件传播的一些详细信息http://wiki.wxpython.org/EventPropagation。或者,您可以使用self.Layout().

于 2012-05-06T15:06:52.367 回答
0

May I suggest that you look into ObjectListView (http://objectlistview.sourceforge.net/python/). This is a wrapper around ListCtrl that has this and many other nice features builtin.
In your case, you could just pass the isSpaceFilling=True parameter to every column and they would divide the space equally. You can even set a minimum width for each column.

ColumnDefn('Title', valueGetter='title', isSpaceFilling=True, minimumWidth=100)

I know ObjectListView looks a bit confusing at first, but I found it very usefull.

于 2012-05-05T22:43:45.580 回答