如何获取wx.ListCtrl及其列名的宽度?

时间:2022-07-22 21:09:14

I'm working in wx.Python and I want to get the columns of my wx.ListCtrl to auto-resize i.e. to be at minimum the width of the column name and otherwise as wide as the widest element or its column name. At first I thought the ListCtrlAutoWidthMixin might do this but it doesn't so it looks like I might have to do it myself (Please correct me if there's a built in way of doing this!!!)

我在wx.Python中工作,我想让我的wx.ListCtrl的列自动调整大小,即至少为列名的宽度,否则与最宽元素或其列名一样宽。起初我以为ListCtrlAutoWidthMixin可能会这样做,但它不是这样看起来我可能必须自己做(请纠正我,如果有一个内置的方式这样做!!!)

How can I find out how wide the titles and elements of my list will be rendered?

如何找出列表的标题和元素的宽度?

4 个解决方案

#1


Yes, you would have to make this yourself for wx.ListCtrl and I'm not sure it would be easy (or elegant) to do right.

是的,你必须自己为wx.ListCtrl做这个,我不确定这样做是否容易(或优雅)。

Consider using a wx.Grid, here is a small example to get you going:

考虑使用wx.Grid,这是一个小例子让你前进:

import wx, wx.grid

class GridData(wx.grid.PyGridTableBase):
    _cols = "This is a long column name,b,c".split(",")
    _data = [
        "1 2 3".split(),
        "4,5,And here is a long cell value".split(","),
        "7 8 9".split()
    ]

    def GetColLabelValue(self, col):
        return self._cols[col]

    def GetNumberRows(self):
        return len(self._data)

    def GetNumberCols(self):
        return len(self._cols)

    def GetValue(self, row, col):
        return self._data[row][col]


class Test(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        grid = wx.grid.Grid(self)
        grid.SetTable(GridData())
        grid.EnableEditing(False)
        grid.SetSelectionMode(wx.grid.Grid.SelectRows)
        grid.SetRowLabelSize(0)
        grid.AutoSizeColumns()


app = wx.PySimpleApp()
app.TopWindow = Test()
app.TopWindow.Show()
app.MainLoop()

#2


In Addition to jakepars answer: this should check, whether the header is bigger, or the item which takes the most space in the column. Not to elegant but working...

除了jakepars之外,还应回答:这应该检查标题是否更大,还是列中占用空间最多的项目。不优雅但工作......

import wx

class Frame(wx.Frame):
    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, *args, **kw)

        self.list = wx.ListCtrl(self, style=wx.LC_REPORT)
        items = ['A', 'b', 'something really REALLY long']
        self.list.InsertColumn(0, "AAAAAAAAAAAAAAAAAAAAAAAA")
        for item in items:
            self.list.InsertStringItem(0, item)
        self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
        a = self.list.GetColumnWidth(0)
        print "a " + str(a)
        self.list.SetColumnWidth(0,wx.LIST_AUTOSIZE_USEHEADER)
        b = self.list.GetColumnWidth(0)
        print "b " + str(b)
        if a>b:
            print "a is bigger"
            self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
app = wx.App(False)
frm = Frame(None, title="ListCtrl test")
frm.Show()
app.MainLoop()

#3


If you'd like to save yourself a lot of headache related to wx.ListCtrl you should switch over to using ObjectListView (has a nice cookbook and forum for code examples). It's very nice and I tend to use it for anything more than a very basic ListCtrl, because it is extremely powerful and flexible and easy to code up. Here's the wxPyWiki page related to it (including example code). The developer is also on the wxPython mailing list so you can email with questions.

如果你想节省很多与wx.ListCtrl相关的头痛,你应该切换到使用ObjectListView(有一个很好的食谱和论坛代码示例)。它非常好用,我倾向于将它用于非常基本的ListCtrl,因为它非常强大,灵活且易于编码。这是与之相关的wxPyWiki页面(包括示例代码)。开发人员也在wxPython邮件列表中,因此您可以通过电子邮件发送问题。

#4


This works for me

这对我有用

import wx

class Frame(wx.Frame):
    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, *args, **kw)

        self.list = wx.ListCtrl(self, style=wx.LC_REPORT)
        items = ['A', 'b', 'something really REALLY long']
        self.list.InsertColumn(0, "AA")
        for item in items:
            self.list.InsertStringItem(0, item)
        self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)

app = wx.App(False)
frm = Frame(None, title="ListCtrl test")
frm.Show()
app.MainLoop()

#1


Yes, you would have to make this yourself for wx.ListCtrl and I'm not sure it would be easy (or elegant) to do right.

是的,你必须自己为wx.ListCtrl做这个,我不确定这样做是否容易(或优雅)。

Consider using a wx.Grid, here is a small example to get you going:

考虑使用wx.Grid,这是一个小例子让你前进:

import wx, wx.grid

class GridData(wx.grid.PyGridTableBase):
    _cols = "This is a long column name,b,c".split(",")
    _data = [
        "1 2 3".split(),
        "4,5,And here is a long cell value".split(","),
        "7 8 9".split()
    ]

    def GetColLabelValue(self, col):
        return self._cols[col]

    def GetNumberRows(self):
        return len(self._data)

    def GetNumberCols(self):
        return len(self._cols)

    def GetValue(self, row, col):
        return self._data[row][col]


class Test(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        grid = wx.grid.Grid(self)
        grid.SetTable(GridData())
        grid.EnableEditing(False)
        grid.SetSelectionMode(wx.grid.Grid.SelectRows)
        grid.SetRowLabelSize(0)
        grid.AutoSizeColumns()


app = wx.PySimpleApp()
app.TopWindow = Test()
app.TopWindow.Show()
app.MainLoop()

#2


In Addition to jakepars answer: this should check, whether the header is bigger, or the item which takes the most space in the column. Not to elegant but working...

除了jakepars之外,还应回答:这应该检查标题是否更大,还是列中占用空间最多的项目。不优雅但工作......

import wx

class Frame(wx.Frame):
    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, *args, **kw)

        self.list = wx.ListCtrl(self, style=wx.LC_REPORT)
        items = ['A', 'b', 'something really REALLY long']
        self.list.InsertColumn(0, "AAAAAAAAAAAAAAAAAAAAAAAA")
        for item in items:
            self.list.InsertStringItem(0, item)
        self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
        a = self.list.GetColumnWidth(0)
        print "a " + str(a)
        self.list.SetColumnWidth(0,wx.LIST_AUTOSIZE_USEHEADER)
        b = self.list.GetColumnWidth(0)
        print "b " + str(b)
        if a>b:
            print "a is bigger"
            self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
app = wx.App(False)
frm = Frame(None, title="ListCtrl test")
frm.Show()
app.MainLoop()

#3


If you'd like to save yourself a lot of headache related to wx.ListCtrl you should switch over to using ObjectListView (has a nice cookbook and forum for code examples). It's very nice and I tend to use it for anything more than a very basic ListCtrl, because it is extremely powerful and flexible and easy to code up. Here's the wxPyWiki page related to it (including example code). The developer is also on the wxPython mailing list so you can email with questions.

如果你想节省很多与wx.ListCtrl相关的头痛,你应该切换到使用ObjectListView(有一个很好的食谱和论坛代码示例)。它非常好用,我倾向于将它用于非常基本的ListCtrl,因为它非常强大,灵活且易于编码。这是与之相关的wxPyWiki页面(包括示例代码)。开发人员也在wxPython邮件列表中,因此您可以通过电子邮件发送问题。

#4


This works for me

这对我有用

import wx

class Frame(wx.Frame):
    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, *args, **kw)

        self.list = wx.ListCtrl(self, style=wx.LC_REPORT)
        items = ['A', 'b', 'something really REALLY long']
        self.list.InsertColumn(0, "AA")
        for item in items:
            self.list.InsertStringItem(0, item)
        self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)

app = wx.App(False)
frm = Frame(None, title="ListCtrl test")
frm.Show()
app.MainLoop()