Photo App – List Control

A window showing a table of data, Name in column 1, File Path in column 2, Hash in column 3.

Now we have a spreadsheet I need to be able to show the results on the screen. Still using wxPython, going to make use of the list control. It doesn’t look great on Windows, but it’s functional and seems to have a lot of flexibility.

Create a ListCtrl

  • Make the list control size=(-1, -1). This will make it take up the full size of the window, and will automatically resize.
  • wx.LC_REPORT: Single or multi-column report view, with optional header.
  • Add the titles to each column. Make them width=-1 as they need to be sized appropriate for the window.


        self.list_ctrl = wx.ListCtrl(panel, size=(-1,-1),
                         style=wx.LC_REPORT
                         |wx.BORDER_SUNKEN
                         )
        self.list_ctrl.InsertColumn(0, 'Name', width=-1)
        self.list_ctrl.InsertColumn(1, 'File Path', width=-1)
        self.list_ctrl.InsertColumn(2, 'Hash', width=-1)
        

Add the list to the sizer

This is what every example does. It works, so just taking it as is, without asking questions.

     sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)

Add the data to the control

  • The data is in a pandas DataForm – see further down this blog for details on this.
  • iterrows() is an iterator through the rows, gives the index, and the row object.
  • First InsertItem to add a line – not sure what these two variables are here, line will always be 0, and index increments.
  • In column 0, add the value of the ‘name’ field. Ditto for ‘path’ and ‘hash’.
  • Increment the index – which will be the row number in the ListCtrl.
        line =  self.index'
        for idx, row in photoList.iterrows():
            self.list_ctrl.InsertItem(self.index, line)
            self.list_ctrl.SetItem(self.index, 0, row['name'])
            self.list_ctrl.SetItem(self.index, 1,  row['path'])
            self.list_ctrl.SetItem(self.index, 2,  row['hash'])
            self.index += 1
        self.list_ctrl.SetColumnWidth(0, wx.LIST_AUTOSIZE)
        self.list_ctrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        self.list_ctrl.SetColumnWidth(2, wx.LIST_AUTOSIZE)
  • After adding all the data, get the columns to resize themselves to fit the data.

Pandas DataFrame

I used test data for this snip. Here is how to read from csv using Pandas:

import pandas

rawData = pandas.read_csv('TestOutput.csv')
photoList = pandas.DataFrame(rawData).sort_values(by='hash')
photoList = photoList[photoList.duplicated('hash', keep=False)]

Use pip install to get your pandas. Don’t forget on mac to use pip3.

The trouble with pandas

On Windows I have installed pandas. But it is still flagging as not found.

After much pip installing and pip uninstalling, somewhere I found a suggestion to use py instead of python.

py .\showlist2.py

This works fine, but if I use the full name python it does not work. I’m pretty sure this has something to do with my path. But I’m not tinkering with that right now.

In fact stack overflow explains it properly.

The trouble with wx.list_ctrl

It looks awful. Really I do not want to even put a screenshot here. It seems that it will look nice on Mac, but on Windows it just looks like a 1980s list.

Looks like wxPython is not so great on looks on Windows. But stack overflow has another suggestions for a wx widget, so I’ll try that.

Need to try ultimate list control: https://wxpython.org/Phoenix/docs/html/wx.lib.agw.ultimatelistctrl.UltimateListCtrl.html

Leave a Reply

Your email address will not be published. Required fields are marked *