无法格式化 WxPython 的 ListCtrl 中的第一列

发布于 2024-09-06 03:56:27 字数 1448 浏览 5 评论 0原文

如果我将 ListCtrl 中第一列的格式设置为居中对齐(或右对齐),则不会发生任何情况。它适用于其他列。

这只发生在 Windows 上 - 我已经在 Linux 上测试过它并且工作正常。 有谁知道是否有解决方法或其他解决方案?

以下是基于 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.list.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()

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.list.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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮华 2024-09-13 03:56:27

我发现这可行(注意我开始在 1 而不是 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)

不知道为什么这可行,但确实如此。希望这样做不会产生任何影响。

感谢 robots.jpg 激发了这个想法。

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.

痴梦一场 2024-09-13 03:56:27

Windows 肯定会以不同的方式对待第一列。一种解决方法是创建一个空列 0 并将其隐藏:

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()

我想不出这样做会产生任何重大副作用,但如果我错了,请告诉我。我想 GetItemText() 或任何其他假设第一列中有有用数据的东西将不再有用。

编辑 - 添加代码以防止调整第 0 列的大小。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文