Newbie wxPython ListCtrl Question

T

Todd7

I am new to python and to wxWindows. I have tried searching google,
reading the documentation and trying to figure out some of the examples,
but I am stumped as to how to get information out of a listctrl at a
certain row and column. I tried to write a simple example to learn to
work with the listctrl as follows:
--------------------------------------
#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
# generated by wxGlade 0.4 on Wed Nov 16 10:23:06 2005

import wx

class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, -1)
self.list_ctrl_1 = wx.ListCtrl(self.panel_1, -1,
style=wx.LC_REPORT|wx.SUNKEN_BORDER)
self.label_1 = wx.StaticText(self.panel_1, -1, "label_1")

self.__set_properties()
self.__do_layout()

self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelected,
self.list_ctrl_1)
# end wxGlade


self.list_ctrl_1.InsertColumn(0,"Name")
self.list_ctrl_1.InsertColumn(1,"Address")
self.list_ctrl_1.SetColumnWidth(0, 215)
self.list_ctrl_1.SetColumnWidth(1, 55)
self.list_ctrl_1.InsertStringItem(0,"John Smith")
self.list_ctrl_1.SetStringItem(0,1, "123 Elm")
self.list_ctrl_1.InsertStringItem(1,"Tom Clark")
self.list_ctrl_1.SetStringItem(1,1, "415 W. 1st")

def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
# end wxGlade

def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2.Add(self.list_ctrl_1, 1, wx.EXPAND, 0)
sizer_2.Add(self.label_1, 0, wx.ADJUST_MINSIZE, 0)
self.panel_1.SetAutoLayout(True)
self.panel_1.SetSizer(sizer_2)
sizer_2.Fit(self.panel_1)
sizer_2.SetSizeHints(self.panel_1)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
self.SetAutoLayout(True)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
sizer_1.SetSizeHints(self)
self.Layout()
# end wxGlade

def OnSelected(self, event): # wxGlade: MyFrame.<event_handler>
self.currentItem = event.m_itemIndex
#What do I put here to get the contents of the Address column?
#I want to do something like self.label_1.SetLabel(?)
#where the ? is the contents of the address column of the
selected row
event.Skip()

# end of class MyFrame


class MyApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
self.SetTopWindow(frame_1)
frame_1.Show()
return 1

# end of class MyApp

if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
 
L

limodou

2005/11/17 said:
I am new to python and to wxWindows. I have tried searching google,
reading the documentation and trying to figure out some of the examples,
but I am stumped as to how to get information out of a listctrl at a
certain row and column. I tried to write a simple example to learn to
work with the listctrl as follows:

I'v made some helper function as that:

def getRowText(self, index, col):
if index >= 0:
return self.list_ctrl_1.GetItem(index, col).GetText()
else:
return ''

def getSelRowText(self, col):
return self.getRowText(self.getSelection(), col)

def getSelection(self):
return self.list_ctrl_1.GetNextItem(-1, wx.LIST_NEXT_ALL,
wx.LIST_STATE_SELECTED)

You can just use self.getSelRowText(1) to gain the second column text
of selection row.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top