Question

Si je mets le format de la première colonne dans un centre ListCtrl aligner (ou Aligné à droite) ne se passe rien. Il travaille pour les autres colonnes.

Cela ne se produit sous Windows - Je l'ai testé sur Linux et il fonctionne très bien. Est-ce que quelqu'un sait s'il y a un tour de travail ou d'une autre solution?

Voici un exemple basé sur le code trouvé à 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()
Était-ce utile?

La solution

J'ai trouvé que cela fonctionne (Remarquez que je commence à insérer les colonnes à 1 au lieu de 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)

Je ne sais pas pourquoi cela fonctionne, mais il le fait. Si tout va bien, il n'y aura pas de répercussion de le faire.

Merci à robots.jpg pour inspirer l'idée.

Autres conseils

Windows traite certainement la première colonne différemment. Une solution consiste à créer une colonne vide 0 et cacher:

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

Je ne peux pas penser à des principaux effets secondaires de le faire, mais laissez-moi savoir si je me trompe. Je suppose que GetItemText () ou toute autre chose qui suppose qu'il existe des données utiles dans la première colonne ne serait plus utile.

Edit -. Code ajouté pour empêcher le redimensionnement colonne 0

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top