ColumnSorterMixin for entries that are dropped into a list ctrl

I was taking a look at this example to see how to update a ListCtrl after items have been dropped into it.

http://stackoverflow.com/a/18965386/784637

I wanted to subclass wx.ListCtrl to use the ColumnSorterMixin like so

class MyListCtrl(wx.ListCtrl, ColumnSorterMixin):

def init(self, parent):

wx.ListCtrl.init(self, parent, style=wx.LC_REPORT)

#ColumnSorterMixin.init(self, ???)

self.index = 0

How do I make the the ListCtrl sortable for each new file I drag and drop into it?

I just wanted to attach the entire file of my code. Merely calling the ColumnSorterMixin constructor prevents a frame from displaying.

dragAndDropListCtrl.py (3.44 KB)

···

On Wednesday, October 23, 2013 4:17:15 AM UTC-4, RedHotChiliPepper wrote:

I was taking a look at this example to see how to update a ListCtrl after items have been dropped into it.

http://stackoverflow.com/a/18965386/784637

I wanted to subclass wx.ListCtrl to use the ColumnSorterMixin like so

class MyListCtrl(wx.ListCtrl, ColumnSorterMixin):

def init(self, parent):

wx.ListCtrl.init(self, parent, style=wx.LC_REPORT)

#ColumnSorterMixin.init(self, ???)

self.index = 0

How do I make the the ListCtrl sortable for each new file I drag and drop into it?

But it gives you an exception if you do so, no? The exception gives you a hint that you can't call it before the list is constructed.

In your case you don't need to call it, at least for me your code works I only needed to uncomment and change this line:

self.SetColumnCount(3) # index? this needs to be the number of columns in the list

and your OnDropFiles method also needs to "return True", ideally it should do only on valid drop target.

I tested on Py 2.7.5 with Phoenix 3.0 relatively current build on Win 7.

Werner

···

On 23/10/2013 10:38, RedHotChiliPepper wrote:

I just wanted to attach the entire file of my code. Merely calling the ColumnSorterMixin constructor prevents a frame from displaying.

Oops, I also had to change "self.SetStringItem(pos, 3, str(file_size))" i.e. add the "str()" as a listctrl does not like int's or long's etc.

Werner

···

On 23/10/2013 11:21, werner wrote:

On 23/10/2013 10:38, RedHotChiliPepper wrote:

I just wanted to attach the entire file of my code. Merely calling the ColumnSorterMixin constructor prevents a frame from displaying.

But it gives you an exception if you do so, no? The exception gives you a hint that you can't call it before the list is constructed.

In your case you don't need to call it, at least for me your code works I only needed to uncomment and change this line:

self.SetColumnCount(3) # index? this needs to be the number of columns in the list

and your OnDropFiles method also needs to "return True", ideally it should do only on valid drop target.

I tested on Py 2.7.5 with Phoenix 3.0 relatively current build on Win 7.

Werner

It does not give an exception. Uncommenting the line

#self.SetColumnCount(self, self.index)

does not resolve my problem, neither does returning “True” for the OnDropFiles method.

Can someone please review the attached file and tell me what I need to do to make sure the ListCtrl is sortable for each new file that is dragged in?

Thanks so much

dragAndDropListCtrl.py (3.42 KB)

···

On Wednesday, October 23, 2013 5:36:41 AM UTC-4, werner wrote:

On 23/10/2013 11:21, werner wrote:

On 23/10/2013 10:38, RedHotChiliPepper wrote:

I just wanted to attach the entire file of my code. Merely calling
the ColumnSorterMixin constructor prevents a frame from displaying.

But it gives you an exception if you do so, no? The exception gives
you a hint that you can’t call it before the list is constructed.

In your case you don’t need to call it, at least for me your code
works I only needed to uncomment and change this line:

self.SetColumnCount(3) # index? this needs to be the number of
columns in the list

and your OnDropFiles method also needs to “return True”, ideally it
should do only on valid drop target.

I tested on Py 2.7.5 with Phoenix 3.0 relatively current build on Win 7.

Werner

Oops, I also had to change “self.SetStringItem(pos, 3, str(file_size))”
i.e. add the “str()” as a listctrl does not like int’s or long’s etc.

Werner

I also wanted to add I’m using 2.9.4.0 msw (classic) and python version 2.7.3 on windows 7

···

On Wednesday, October 23, 2013 7:06:53 PM UTC-4, RedHotChiliPepper wrote:

It does not give an exception. Uncommenting the line

#self.SetColumnCount(self, self.index)

does not resolve my problem, neither does returning “True” for the OnDropFiles method.

Can someone please review the attached file and tell me what I need to do to make sure the ListCtrl is sortable for each new file that is dragged in?

Thanks so much

On Wednesday, October 23, 2013 5:36:41 AM UTC-4, werner wrote:

On 23/10/2013 11:21, werner wrote:

On 23/10/2013 10:38, RedHotChiliPepper wrote:

I just wanted to attach the entire file of my code. Merely calling
the ColumnSorterMixin constructor prevents a frame from displaying.

But it gives you an exception if you do so, no? The exception gives
you a hint that you can’t call it before the list is constructed.

In your case you don’t need to call it, at least for me your code
works I only needed to uncomment and change this line:

self.SetColumnCount(3) # index? this needs to be the number of
columns in the list

and your OnDropFiles method also needs to “return True”, ideally it
should do only on valid drop target.

I tested on Py 2.7.5 with Phoenix 3.0 relatively current build on Win 7.

Werner

Oops, I also had to change “self.SetStringItem(pos, 3, str(file_size))”
i.e. add the “str()” as a listctrl does not like int’s or long’s etc.

Werner

Hi Oops,

Should have done a bit more testing, attached an update.

Not quit there yet as "self.itemDataMap" needs to be updated when you drag a file for the sorting to really work.

I would suggest to refactor your code, maybe have your "dropUpdate" method in your FileWindow class or .......?

To figure out what was missing I looked at the wxPython demo for the listctrl, note that the "import images" is the one from the wxPython demo.

Hope this helps
Werner

dragAndDropListCtrl.py (3.92 KB)

Hi Werner -

The update does not solve my problem. I still can’t sort by column. Are you able to sort by the column?

You added some code for images, but I am not sure how this relates to actually solving my problem. I was able to install the Python Imaging Library (PIL) here http://www.pythonware.com/products/pil/ and import it like so

from PIL import Image

but I am not sure what module

import images

refers to.

Since this thread is getting long I just wanted to confirm the problem is not resolved and I would like to have newly added entries to a ListCtrl be sortable. I’m attaching a copy of the current code I have and would really appreciate anyone’s help.

dragAndDropListCtrl.py (3.42 KB)

···

On Thursday, October 24, 2013 3:04:44 AM UTC-4, werner wrote:

Hi Oops,

Should have done a bit more testing, attached an update.

Not quit there yet as “self.itemDataMap” needs to be updated when you
drag a file for the sorting to really work.

I would suggest to refactor your code, maybe have your “dropUpdate”
method in your FileWindow class or …?

To figure out what was missing I looked at the wxPython demo for the
listctrl, note that the “import images” is the one from the wxPython demo.

Hope this helps

Werner

Hi Werner -

The update does not solve my problem.

As I had mentioned it is not solved until "self.itemDataMap" directory is updated when you drop a file.

I still can't sort by column. Are you able to sort by the column?

If you click on the column headers BEFORE dropping the first file you can now see that the up/down arrows show to indicate sort order.

If you click AFTER you drop a file you will get an exception as "self.itemDatamap" is still empty and does not contain the data of the dropped item.

You added some code for images, but I am not sure how this relates to actually solving my problem.

Don't you want an indication what the sort order is?

I also moved the "columnSorterMixin" to your FileWindow class and it gets initialized - I did that based on what I saw in the wxPython demo for the listctrl.

I would just do a compare of my version against yours then you see all changes.

I was able to install the Python Imaging Library (PIL) here http://www.pythonware.com/products/pil/ and import it like so

    from PIL import Image

You don't need PIL.

but I am not sure what module

    import images

I thought I mentioned that "images.py" is the one delivered with the wxPython demo and it contains the up/down images.

If you haven't already done so I would suggest that you look at the ListCtrl demo which is delivered with the wxPython demo.

Hope this helps
Werner

···

On 24/10/2013 10:51, RedHotChiliPepper wrote:

I would create a global ordered dict to try to solve all disputes and changes when run across.
You would have to utilize the global Keyword and also keep track of what you want to be first/last and also a py27 OrderedDict would be in order for this matter.

ListCtrls and TreeCtrls can be a bit tricky for this depending on what the desired end affect will be upon dropping the target.
The wxPyWiki has a bunch of examples(maybe older(code)) for List/Tree Ctrl specific stuff and it also has Drap and Drop Stuff, which is probably the problem you want to customize anyway…

Drag and Drop can be tricky. First you need to know (everything Type) that was dropped.
Ex.
for DropTarget in dir(wx):
if DropTarget.startswith(‘DT_’):
print('DropTarget = ', DropTarget)

Then proceed thru a list of options of types as to which need processed, which then are sent to specific functions dealing with each type.

···

On Thursday, October 24, 2013 4:07:56 AM UTC-5, werner wrote:

On 24/10/2013 10:51, RedHotChiliPepper wrote:

Hi Werner -

The update does not solve my problem.

As I had mentioned it is not solved until “self.itemDataMap” directory
is updated when you drop a file.

I still can’t sort by column. Are you able to sort by the column?

If you click on the column headers BEFORE dropping the first file you
can now see that the up/down arrows show to indicate sort order.

If you click AFTER you drop a file you will get an exception as
“self.itemDatamap” is still empty and does not contain the data of the
dropped item.

You added some code for images, but I am not sure how this relates to
actually solving my problem.

Don’t you want an indication what the sort order is?

I also moved the “columnSorterMixin” to your FileWindow class and it
gets initialized - I did that based on what I saw in the wxPython demo
for the listctrl.

I would just do a compare of my version against yours then you see all
changes.

I was able to install the Python Imaging Library (PIL) here
http://www.pythonware.com/products/pil/ and import it like so

from PIL import Image

You don’t need PIL.

but I am not sure what module

import images

I thought I mentioned that “images.py” is the one delivered with the
wxPython demo and it contains the up/down images.

If you haven’t already done so I would suggest that you look at the
ListCtrl demo which is delivered with the wxPython demo.

Hope this helps

Werner

Hi,

Attached is now a working version (with 2.9.4 or 3.0 Phoenix for me).

I moved the drop handler to FileWindow class as it made things easier.

You still need to get 'images.py' from the wxPython or use another source for the 'up/down' images.

Werner

dragAndDropListCtrl.py (4.15 KB)

Werner - thanks so much! This seems to be working great. Thanks for clarifying I needed images.py from the wxpython demo, I thought I needed to install the Python Imaging Library.

-s

···

On Thursday, October 24, 2013 8:53:00 AM UTC-4, werner wrote:

Hi,

Attached is now a working version (with 2.9.4 or 3.0 Phoenix for me).

I moved the drop handler to FileWindow class as it made things easier.

You still need to get ‘images.py’ from the wxPython or use another
source for the ‘up/down’ images.

Werner