python - how to get the information of an item in wxListCtrl? -
i have list generated using wxlistctrl has 3 coloumns. data generated when list updates in need use in other part of code.can 1 please tell me how values of item of 3 coloumns in list? list below...
self.list_ctrl = wx.listctrl(self.panel, size=(565,150),pos=(15,20),style=wx.lc_report | wx.border_sunken) self.name=self.list_ctrl.insertcolumn(0, 'task name',width=189) self.date=self.list_ctrl.insertcolumn(1, 'run ',width=189) self.status=self.list_ctrl.insertcolumn(2, 'status', width=187 self.index=0 where items generated using..
taskname=self.list_ctrl.insertstringitem(self.index,task) taskdate=self.list_ctrl.setstringitem(self.index, 1,strftime("%d-%m-%y", gmtime())) tasktime=self.list_ctrl.setstringitem(self.index,2,datetime.now().strftime('%h:%m:%s')) i can able name of item i.e, 'self.name' under 1st coloumn using
name=self.list_ctrl.getitemtext(self.name) but 'self.date' , 'self.time' returning int type values.how can date , time in variable 'taskdate' , 'tasktime' respectively?
there couple of ways this. simplest (in opinion) associate objects each row, we'll "hard" way first:
import wx ######################################################################## class myform(wx.frame): #---------------------------------------------------------------------- def __init__(self): wx.frame.__init__(self, none, wx.id_any, "list control tutorial") # add panel looks correct on platforms panel = wx.panel(self, wx.id_any) self.index = 0 self.list_ctrl = wx.listctrl(panel, size=(-1,100), style=wx.lc_report |wx.border_sunken ) self.list_ctrl.insertcolumn(0, 'subject') self.list_ctrl.insertcolumn(1, 'due') self.list_ctrl.insertcolumn(2, 'location', width=125) btn = wx.button(panel, label="add line") btn2 = wx.button(panel, label="get data") btn.bind(wx.evt_button, self.add_line) btn2.bind(wx.evt_button, self.getcolumn) sizer = wx.boxsizer(wx.vertical) sizer.add(self.list_ctrl, 0, wx.all|wx.expand, 5) sizer.add(btn, 0, wx.all|wx.center, 5) sizer.add(btn2, 0, wx.all|wx.center, 5) panel.setsizer(sizer) #---------------------------------------------------------------------- def add_line(self, event): line = "line %s" % self.index self.list_ctrl.insertstringitem(self.index, line) self.list_ctrl.setstringitem(self.index, 1, "01/19/2010") self.list_ctrl.setstringitem(self.index, 2, "usa") self.index += 1 #---------------------------------------------------------------------- def getcolumn(self, event): """""" count = self.list_ctrl.getitemcount() cols = self.list_ctrl.getcolumncount() row in range(count): col in range(cols): item = self.list_ctrl.getitem(itemid=row, col=col) print item.gettext() #---------------------------------------------------------------------- # run program if __name__ == "__main__": app = wx.app(false) frame = myform() frame.show() app.mainloop() this modified earlier answer similar question. anyway, let's take @ how use objects instead:
import wx ######################################################################## class car(object): """""" #---------------------------------------------------------------------- def __init__(self, make, model, year, color="blue"): """constructor""" self.make = make self.model = model self.year = year self.color = color ######################################################################## class mypanel(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) rows = [car("ford", "taurus", "1996"), car("nissan", "370z", "2010"), car("porche", "911", "2009", "red") ] self.list_ctrl = wx.listctrl(self, size=(-1,100), style=wx.lc_report |wx.border_sunken ) self.list_ctrl.bind(wx.evt_list_item_selected, self.onitemselected) self.list_ctrl.insertcolumn(0, "make") self.list_ctrl.insertcolumn(1, "model") self.list_ctrl.insertcolumn(2, "year") self.list_ctrl.insertcolumn(3, "color") index = 0 self.myrowdict = {} row in rows: self.list_ctrl.insertstringitem(index, row.make) self.list_ctrl.setstringitem(index, 1, row.model) self.list_ctrl.setstringitem(index, 2, row.year) self.list_ctrl.setstringitem(index, 3, row.color) self.myrowdict[index] = row index += 1 sizer = wx.boxsizer(wx.vertical) sizer.add(self.list_ctrl, 0, wx.all|wx.expand, 5) self.setsizer(sizer) #---------------------------------------------------------------------- def onitemselected(self, event): """""" currentitem = event.m_itemindex car = self.myrowdict[currentitem] print car.make print car.model print car.color print car.year ######################################################################## class myframe(wx.frame): """""" #---------------------------------------------------------------------- def __init__(self): """constructor""" wx.frame.__init__(self, none, wx.id_any, "list control tutorial") panel = mypanel(self) self.show() #---------------------------------------------------------------------- if __name__ == "__main__": app = wx.app(false) frame = myframe() app.mainloop() here create list of car objects , use dot-notation add properties of class listctrl. when select item list, grab selected item event object , using dictionary. not simple, rather it. can read more along other tips , tricks here.
however, think best solution use objectlistview (a listctrl wrapper) makes rows true objects , allows easier access values, bringing in bunch of other enhancements. sadly it's not part of normal wxpython distribution of yet, it's easy add pypi. can read more in article blog too!
Comments
Post a Comment