Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make column width take up available space in wxPython ListCtrl

I have three columns in my wx.ListCtrl(size=(-1,200)). I would like the columns to fill up the width of the ListCtrl after its created. Ideally, the first column can expand to fill up the extra space available. The second and third columns don't need to expand, and preferably will not change in width (formatting ocd).

Currently, each ListCtrl column is set up using (width=-1).

I have a feeling I can use this section of the code to my advantage...

# Expand first column to fit longest entry item
list_ctrl.SetColumnWidth(0, wx.LIST_AUTOSIZE)

Pseudo-code (perhaps):

# After wx.ListCtrl creation
Get width of ListCtrl control
Get width of each ListCtrl column
Calculate unused width of ListCtrl
Set first column width to original width + unused width

Added:

Given the following example, I don't understand how to initiate the autowidthmixin. Currently, I am trying to put the listctrl inside a foldpanel. The foldpanel is a class and a function within the class creates the listctrl. I am not even confident that this can be done given the structure of my code at the moment!

class MyPanel(wx.Panel):

    def __init__(self, parent, dictionary):
        self.dictionary = dictionary
        """Constructor"""
        wx.Panel.__init__(self, parent)

        # Layout helpers (sizers) and content creation (setPanel)
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.mainSizer)
        list_ctrl = self.setPanel()
        self.mainSizer.Add(list_ctrl, 0, wx.ALL | wx.EXPAND, 5)
        self.GetSizer().SetSizeHints(self)

    def setPanel(self):
        index = 0

        list_ctrl = wx.ListCtrl(self, size=(-1, 200),
                                style=wx.LC_REPORT | wx.BORDER_SUNKEN)

        list_ctrl.InsertColumn(0, "Variable", format=wx.LIST_FORMAT_LEFT, width=-1)
        list_ctrl.InsertColumn(1, "x", format=wx.LIST_FORMAT_RIGHT, width=-1)
        list_ctrl.InsertColumn(2, u"\u03D0", format=wx.LIST_FORMAT_RIGHT, width=-1)

        for key, value in self.dictionary.iteritems():
            list_ctrl.InsertStringItem(index, str(key))
            list_ctrl.SetStringItem(index, 1, ("%.2f" % value[0]))
            list_ctrl.SetStringItem(index, 2, ("%.8f" % value[1]))
            index += 1

        list_ctrl.SetColumnWidth(0, wx.LIST_AUTOSIZE)
        list_ctrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        list_ctrl.SetColumnWidth(2, wx.LIST_AUTOSIZE)

        return list_ctrl
like image 633
Michael Markieta Avatar asked Jul 03 '12 15:07

Michael Markieta


1 Answers

You need to use the ListCtrlAutoWidthMixin mixin class. The wxPython demo application has an example in the ListCtrl demo. According to the documentation, you can use its setResizeColumn method to tell it which column to resize. The default is the last column.

EDIT (07/05/2012): In your code, create a ListCtrl class similar to the one in the demo. It would look something like this:

    class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    def __init__(self, parent, ID, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
        listmix.ListCtrlAutoWidthMixin.__init__(self)
        self.setResizeColumn(0)

Then when you instantiate it, you'd just call list_ctrl = TestListCtrl(arg1, arg2...argN)

Note that I included a call to setResizeColumn() in my code above. It's not tested, but it should work.

like image 108
Mike Driscoll Avatar answered Sep 22 '22 10:09

Mike Driscoll