无法格式化 WxPython 的 ListCtrl 中的第一列
如果我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现这可行(注意我开始在 1 而不是 0 处插入列):
不知道为什么这可行,但确实如此。希望这样做不会产生任何影响。
感谢 robots.jpg 激发了这个想法。
I have found that this works (notice I start inserting the columns at 1 rather than 0):
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 肯定会以不同的方式对待第一列。一种解决方法是创建一个空列 0 并将其隐藏:
我想不出这样做会产生任何重大副作用,但如果我错了,请告诉我。我想 GetItemText() 或任何其他假设第一列中有有用数据的东西将不再有用。
编辑 - 添加代码以防止调整第 0 列的大小。
Windows definitely treats the first column differently. One workaround is to create an empty column 0 and hide it:
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.