简体   繁体   中英

ListCtrl to adjust window in wxPython

I've been creating an app using wxPython. I'd like to display some data using ListCtrl and I'd like the colums occuping maximum space. This is: the maximum size available is divided between the rows I have. I've tried this but it doesn't work (this is the panel where listctrl is):

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)

Now, it doesn't even occupy full window, it only shows a small square on top left corner. If I comment "self.Bind(wx.EVT_SIZE..." line, it shows full screen, but columns are small.

Thank you in advance!!!

What you are missing is event.Skip() call in the OnResize function. Some details about event propagation can be found here http://wiki.wxpython.org/EventPropagation . Alternatively you could use self.Layout() .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM