views:

1125

answers:

2

I have a ListCtrl that displays a list of items for the user to select. This works fine except that when the ctrl is not large enough to show all the items, I want it to expand downwards with a vertical scoll bar rather than using a horizontal scroll bar as it expands to the right.

The ListCtrl's creation:

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

Items are inserted using wx.ListItem:

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

Use the wxLC_REPORT style.

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()
Toni Ruža
usering wx.LC_REPORT instead of wx.LC_LIST gives me a vertical scroll bar, but all the text items dissapear (although the scroll bar looks about the right size for a complete list...)
Fire Lancer
You have to explicitly insert the columns before you can use them. I added an example
Toni Ruža
why is your range for i incrementing by 5?
Soviut
so that the i+j in the inner loop would go from 0 to 99, it's just a way to generate different cell values.
Toni Ruža
+1  A: 

Try this:

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()
Toni Ruža
ok, but how do I set the width of each column? (eg 200px?)
Fire Lancer
The manual says that self.test.SetColumnWidth(-1, 200) should work but it doesn't, so I don't know. SetColumnWidth works for report mode though, but there you have the hassle of managing columns yourself.
Toni Ruža
Or... you can use just one column... maybe even use wxListbox... why complicate things?
Toni Ruža