Formatting 无法格式化WxPython';s ListCtrl
如果我将ListCtrl中第一列的格式设置为居中对齐(或向右对齐),则不会发生任何情况。它适用于其他列 这只发生在Windows上——我已经在Linux上测试过了,效果很好。 有人知道是否有一个工作循环或其他解决方案吗 下面是一个基于在上找到的代码的示例Formatting 无法格式化WxPython';s ListCtrl,formatting,wxpython,listctrl,Formatting,Wxpython,Listctrl,如果我将ListCtrl中第一列的格式设置为居中对齐(或向右对齐),则不会发生任何情况。它适用于其他列 这只发生在Windows上——我已经在Linux上测试过了,效果很好。 有人知道是否有一个工作循环或其他解决方案吗 下面是一个基于在上找到的代码的示例 Windows肯定会以不同的方式对待第一列。一种解决方法是创建空列0并将其隐藏: class Actresses(wx.Frame): def __init__(self, parent, id, title): wx.
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的大小。我发现这是可行的(注意,我开始在1而不是0处插入列): 不知道为什么会这样,但确实如此。希望这样做不会产生任何影响
感谢robots.jpg激发了这个想法。我注意到您的示例和我的应用程序中都有相同的行为。也许这是Windows平台的一个限制-看看你是否可以找到其他程序,其中的列表第一列是对齐的?这是一个好主意,而且很有效;但是,用户可以调整隐藏列的大小。我认为这将是一个问题。您的回答给了我一个想法-如果我添加了一个虚拟列,然后在添加其他列后将其删除,该怎么办?要解决这个问题,您可以捕获列拖动事件并否决它,如果它位于列0上(将此添加到我的回答中)。不过,您的解决方案因简单而获胜。我不知道您可以像那样跳过索引0,但仍然可以从SetStringItem之类的函数以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()
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)