wxpythonのlistctrlで最初の列をフォーマットできません
-
28-09-2019 - |
質問
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()
解決
私はこれが機能することを発見しました(0ではなく1で列を挿入し始めることに注意してください):
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に感謝します。
他のヒント
Windowsは間違いなく最初の列を異なって扱います。 1つの回避策は、空の列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のサイズ変更を防ぐためのコードが追加されました。
所属していません StackOverflow