I have a wxpython listctrl , which contains 4 column[A, B, C, D]. User selects any one of the row from listctrl. Now i have a button in my gui so when i click that i want to print the value of column D from that selected row.
For example lets user has selected this row:
[PYTHON, JAVA, MATLAB, RUBY]
now if user clicks the button it should give output: RUBY
I am binding THE BUTTON in this way
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnPlot, self.list)
self.list.Append((j[0],j[1],j[2],j[3])) #values are inserted in the listctrl
And OnPlot event i have defined as:
def OnPlot(self, event):
click = event.GetText()
It doesn't work but. How can i accomplish this?
The event will pass the index of the selected item from the listctrl.
Use the index to get the item and column within the listctrl, in this case 3.
Get the text of the column.
Namely:
def on_plot(self, event):
ind = event.GetIndex()
item = self.list_ctrl.GetItem(ind,3)
print item.GetText()
This assumes that you have bound the listctrl like this:
self.list_ctrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_plot, self.list_ctrl)
Edit:
With regard to your comment and the edit to the title of the question.
You cannot bind an arbitrary event to something. A button has a set of defined events as does a listctrl and never the twain shall meet. The way around your issue is to bind the button to a button event.
plot_button.Bind(wx.EVT_BUTTON, self.on_plot)
then define on_plot
like this:
def on_plot(self, event):
ind = self.list_ctrl.GetFirstSelected()
if ind >=0:
item = self.list_ctrl.GetItem(ind,3)
print item.GetText()
As you will see it achieves the same end result but and it's a big but, you have already had to click on the listctrl to select an item. That being the case, you could have already achieved the same result, using the first method, without having to define the button in the first place and using this method you now have to check in case nothing was selected (ind must not be -1).