views:

93

answers:

1

I'm new to Pythong and I have been trying to get a button within UltimateListCtrl. I still can't figure out what I'm doing wrong. Here is my code:

try:
    from agw import ultimatelistctrl as ULC
except ImportError: # if it's not there locally, try the wxPython lib.
    from wx.lib.agw import ultimatelistctrl as ULC 


self.table = ULC.UltimateListCtrl(self, -1, agwStyle=ULC.ULC_REPORT|
                                            ULC.ULC_HAS_VARIABLE_ROW_HEIGHT)

self.table.InsertColumn(0, "Name")
self.table.InsertColumn(1, "Size")
self.table.InsertColumn(2, "Download")


for i in range(0, len(masterlist)):
    pos = self.table.InsertStringItem(i,str(masterlist[i]['name']))
    self.table.SetStringItem(pos, 1,str(masterlist[i]['size']))
    button = wx.Button(self, id=i, label="Download")
    self.table.SetItemWindow(pos, col=2, wnd=button, expand=True)

masterlist is a list of download items.

I get this traceback:

Traceback (most recent call last):
File "E:\TestApp.py", line 67, in Display
self.table.SetItemWindow(pos, col=5, wnd=button, expand=True)
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\agw\ultimatelistctrl.py", line 12961, in SetItemWindow
return self._mainWin.SetItemWindow(item, wnd, expand)
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\agw\ultimatelistctrl.py", line 9021, in SetItemWindow
item.SetWindow(wnd, expand)
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\agw\ultimatelistctrl.py", line 1863, in SetWindow
mainWin = listCtrl._mainWin
AttributeError: 'MainWindow' object has no attribute '_mainWin'
+2  A: 

button's parent should be your ULC i.e self.table

So change this line:

button = wx.Button(self, id=wx.ID_ANY, label="Download")

to this:

button = wx.Button(self.table, id=wx.ID_ANY, label="Download")

Update in response to comment:

For some reason it doesn't seem to be possible to delete all items in a ULC with the DeleteAllItems() method if any of the items contain widgets so instead use DeleteItem().

def emptyList(self)
    itemCount = self.list.GetItemCount()
    for item in xrange(itemCount):
        self.list.DeleteItem(0)
volting
Thanks, that worked great.
FlowofSoul
Glad to here it!
volting
When I try to "self.table.DeleteAllItems()" I get this error message: wx._core.PyDeadObjectError: The C++ part of the Button object has been deleted, attribute access no longer allowed. Do you know how to fix this? I don't see the point of opening up another question.
FlowofSoul
I dont why but it *seems* that if your items contain widgets you must delete them with `DeleteItem()`, Ill add an example of how Iv'e done it to my answer.
volting
@FlowOfSoul: The above mentioned alternative for emptying a `ULC` might work fine for you, however if have a large list and speed is concern then it might be worth your while asking on the wxpython mailing list to see if the error is the normal behaviour or a bug, as using the `DeleteItem()` method is slower then `DeleteAllItems()` because it sends the EVT_LIST_DELETE_ITEM event (as mentioned in the documentaion)
volting