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

发布于 2024-07-16 09:05:51 字数 196 浏览 5 评论 0原文

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

我怎样才能知道我的列表的标题和元素会被渲染吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

这个俗人 2024-07-23 09:05:51

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

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...

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()
信仰 2024-07-23 09:05:51

如果您想避免与 wx.ListCtrl 相关的很多麻烦,您应该转而使用 ObjectListView< /a> (有一本很好的食谱和代码示例论坛)。 它非常好,我倾向于将它用于除非常基本的 ListCtrl 以外的任何用途,因为它非常强大、灵活且易于编码。 这是与之相关的wxPyWiki 页面(包括示例代码)。 开发人员也在 wxPython 邮件列表中,因此您可以通过电子邮件提出问题。

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.

-黛色若梦 2024-07-23 09:05:51

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

考虑使用 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()

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.

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

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()
踏雪无痕 2024-07-23 09:05:51

这对我有用

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文