Reputation: 662
I have been trying to figure out how to use wx.UltimateListCtrl in Python to create a customized widget. Based on some internet examples I have this basic script but i'm stuck in how to bind events inside the widget in order to get the stringtext from column 1 if checkBox in column two is selected. This is the code:
import sys
import wx
import wx.lib.agw.ultimatelistctrl as ULC
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "UltimateListCtrl Demo")
list = ULC.UltimateListCtrl(self, wx.ID_ANY, agwStyle=ULC.ULC_HAS_VARIABLE_ROW_HEIGHT|wx.LC_REPORT|wx.LC_VRULES|wx.LC_HRULES|wx.LC_SINGLE_SEL)
list.InsertColumn(0, "File Name")
list.InsertColumn(1, "Select")
for _ in range(4):
index = list.InsertStringItem(sys.maxint, "Item " + str(_))
list.SetStringItem(index, 1, "")
checkBox = wx.CheckBox( list, wx.ID_ANY, u"", wx.DefaultPosition, wx.DefaultSize, 0 )
list.SetItemWindow(index, 1, checkBox , expand=True)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(list, 1, wx.EXPAND)
self.SetSizer(sizer)
checkBox1.Bind( wx.EVT_CHECKBOX, checkBoxOnCheckBox )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def checkBoxOnCheckBox( self, event ):
print 'Yes'
#event.Skip()
app = wx.PySimpleApp()
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
Thanks in advance for your help Ivo
Upvotes: 1
Views: 883
Reputation: 386
First of all: Try not to name the ULC list as this masks the Python list.
There are of course multiple ways to do what you want. One solution is to keep a reference of the checkbox and link it with the index of the item. This way you can identify the item.
I hope this helps.
import sys
import wx
import wx.lib.agw.ultimatelistctrl as ULC
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "UltimateListCtrl Demo")
agwStyle = (ULC.ULC_HAS_VARIABLE_ROW_HEIGHT | wx.LC_REPORT |
wx.LC_VRULES | wx.LC_HRULES | wx.LC_SINGLE_SEL)
self.mylist = mylist = ULC.UltimateListCtrl(self, wx.ID_ANY,
agwStyle=agwStyle)
mylist.InsertColumn(0, "File Name")
mylist.InsertColumn(1, "Select")
self.checkboxes = {}
for _ in range(4):
index = mylist.InsertStringItem(sys.maxint, "Item " + str(_))
mylist.SetStringItem(index, 1, "")
checkBox = wx.CheckBox(mylist, wx.ID_ANY, u"", wx.DefaultPosition,
wx.DefaultSize, 0)
self.checkboxes[checkBox.GetId()] = index
mylist.SetItemWindow(index, 1, checkBox, expand=True)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(mylist, 1, wx.EXPAND)
self.SetSizer(sizer)
self.Bind(wx.EVT_CHECKBOX, self.checkBoxOnCheckBox)
def __del__(self):
pass
# Virtual event handlers, overide them in your derived class
def checkBoxOnCheckBox(self, event):
cb = event.GetEventObject()
idx = self.checkboxes[cb.GetId()]
print(self.mylist.GetItemText(idx))
print(cb.GetValue())
event.Skip()
app = wx.PySimpleApp()
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
Upvotes: 1