wxpython - 垂直扩展列表控件而非水平扩展

2 投票
2 回答
4206 浏览
提问于 2025-04-11 09:31

我有一个列表控件(ListCtrl),它用来显示一系列供用户选择的项目。这个功能正常,但当控件的大小不够显示所有项目时,我希望它能向下扩展,并出现一个垂直滚动条,而不是向右扩展并出现一个水平滚动条。

这个列表控件是这样创建的:

self.subjectList = wx.ListCtrl(self, self.ID_SUBJECT, style = wx.LC_LIST | wx.LC_SINGLE_SEL | wx.LC_VRULES)

项目是通过 wx.ListItem 插入的:

item = wx.ListItem()
item.SetText(subject)
item.SetData(id)
item.SetWidth(200)
self.subjectList.InsertItem(item)

2 个回答

1

试试这个:

import wx

class Test(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.test = wx.ListCtrl(self, style = wx.LC_ICON | wx.LC_AUTOARRANGE)

        for i in range(100):
            self.test.InsertStringItem(self.test.GetItemCount(), str(i))

        self.Show()

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

使用 wxLC_REPORT 这种样式。

import wx

class Test(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.test = wx.ListCtrl(self, style = wx.LC_REPORT | wx.LC_NO_HEADER)

        for i in range(5):
            self.test.InsertColumn(i, 'Col %d' % (i + 1))
            self.test.SetColumnWidth(i, 200)


        for i in range(0, 100, 5):
            index = self.test.InsertStringItem(self.test.GetItemCount(), "")
            for j in range(5):
                self.test.SetStringItem(index, j, str(i+j)*30)

        self.Show()

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

撰写回答