开发者

Cannot format first column in WxPython's ListCtrl

If I set the format of the first column in a ListCtrl to align centre (or align right) nothing happens. It works for the other columns.

This only happens on Windows - I have tested it on Linux and it works fine. Does anyone know if there is a work-round or other solution?

Here is an example based on code found at http://zetcode.com/wxpython/

import wx
import sys

packages = [('jessica alba', 'pomona', '1981'), ('sigourney weaver', 'new york',    '1949'),
    ('angelina jolie', 'los angeles', '1975'), ('natalie portman', 'jerusalem', '1981'),
    ('rachel weiss', 'london', '1971'), ('scarlett johansson', 'new york', '1984' )]

class Actresses(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(380, 230))

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        panel = wx.Panel(self, -1)

        self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT)
        self.list.InsertColumn(0, 'name', wx.LIST_FORMAT_CENTRE,width=140)
        self.li开发者_StackOverflowst.InsertColumn(1, 'place', wx.LIST_FORMAT_CENTRE,width=130)
        self.list.InsertColumn(2, 'year', wx.LIST_FORMAT_CENTRE, 90)

        for i in packages:
            index = self.list.InsertStringItem(sys.maxint, i[0])
            self.list.SetStringItem(index, 1, i[1])
            self.list.SetStringItem(index, 2, i[2])

        hbox.Add(self.list, 1, wx.EXPAND)
        panel.SetSizer(hbox)

        self.Centre()
        self.Show(True)

app = wx.App()
Actresses(None, -1, 'actresses')
app.MainLoop()


I have found that this works (notice I start inserting the columns at 1 rather than 0):

    self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT)
    self.list.InsertColumn(1, 'name', wx.LIST_FORMAT_CENTRE,width=140)
    self.list.InsertColumn(2, 'place', wx.LIST_FORMAT_CENTRE,width=130)
    self.list.InsertColumn(3, 'year', wx.LIST_FORMAT_CENTRE, 90)

Not sure why this works, but it does. Hopefully, there will be no repercussions from doing this.

Thanks to robots.jpg for inspiring the idea.


Windows definitely treats the first column differently. One workaround is to create an empty column 0 and hide it:

class Actresses(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(380, 230))
        #...

        self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT)
        self.list.InsertColumn(0, '', width=0)
        self.list.InsertColumn(1, 'name', wx.LIST_FORMAT_CENTRE,width=140)
        self.list.InsertColumn(2, 'place', wx.LIST_FORMAT_CENTRE,width=130)
        self.list.InsertColumn(3, 'year', wx.LIST_FORMAT_CENTRE, width=90)

        for i in packages:
            index = self.list.InsertStringItem(sys.maxint, '')
            self.list.SetStringItem(index, 1, i[0])
            self.list.SetStringItem(index, 2, i[1])
            self.list.SetStringItem(index, 3, i[2])

        # catch resize event
        self.list.Bind(wx.EVT_LIST_COL_BEGIN_DRAG, self.OnColDrag)
        #...

    def OnColDrag(self, evt):
        if evt.m_col == 0:
            evt.Veto()

I can't think of any major side-effects from doing this, but let me know if I'm wrong. I guess GetItemText() or anything else that assumes there is useful data in the first column would no longer be useful.

Edit - added code to prevent resizing column 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜