簡體   English   中英

ListCtrl調整wxPython中的窗口

[英]ListCtrl to adjust window in wxPython

我一直在使用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 ...”行,則顯示全屏,但列很小。

先感謝您!!!

您缺少的是OnResize函數中的event.Skip()調用。 有關事件傳播的一些詳細信息,請參見http://wiki.wxpython.org/EventPropagation 或者,您可以使用self.Layout()

我可以建議您研究一下ObjectListView(http://objectlistview.sourceforge.net/python/)。 這是ListCtrl的包裝,它具有內置的此功能和許多其他不錯的功能。
在您的情況下,您可以將isSpaceFilling=True參數傳遞給每一列,它們將平均划分空間。 您甚至可以為每列設置最小寬度。

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

我知道ObjectListView乍看起來有點混亂,但是我發現它非常有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM