Getting selected item in a ListBox, in a EVT_CONTEXT_MENU event

735 views
Skip to first unread message

cappy2112

unread,
Jan 3, 2010, 6:52:30 PM1/3/10
to wxPython-users

I've created a right-click menu for a ListBox. The right-click menu
allows the user to Save/Restore the
items in the ListBox to/from disk. The popup menu works fine and the
event handler is called.

When I look at the event object, there are two methods which imply
they will return the selected item:
event.GetSelection() and event.GetString() return 0 and ""
respectively, not the selected item.

event.GetEventObject() does not have any methods which look like they
should return the selected item.

So I've started looking through the wx API under Menu and Event
related topics, but I'm obviously in the wrong place.

I could probably store the selected item in a class variable during
the EVT_LISTBOX event, then just reference the class variable in the
EVT_CONTEXT_MENU event handler, but I would like to better-understand
the event structure of the popup menu event.

Josh English

unread,
Jan 3, 2010, 7:39:47 PM1/3/10
to wxpytho...@googlegroups.com
The event should be a menu event, not a listbox event, so you'd need
to use the ListBox's GetCurrentSelection() method, or some variation
thereof, to find the data you want.

Josh

> --
> To unsubscribe, send email to wxPython-user...@googlegroups.com
> or visit http://groups.google.com/group/wxPython-users?hl=en

--
Josh English
Joshua.R...@gmail.com
http://joshenglish.livejournal.com

cappy2112

unread,
Jan 3, 2010, 10:58:45 PM1/3/10
to wxPython-users

On Jan 3, 4:39 pm, Josh English <joshua.r.engl...@gmail.com> wrote:
> The event should be a menu event, not a listbox event, so you'd need
> to use the ListBox's GetCurrentSelection() method, or some variation
> thereof, to find the data you want.

Thanks Josh...

When I use the ListBox GetCurrentSelection(), GetLabel() and
GetLabelText() none of them return the selected item, while
in the context of the menu event. I have the style for the ListBox set
to LB_SINGLE, FWIW.

Robin Dunn

unread,
Jan 4, 2010, 8:07:31 PM1/4/10
to wxpytho...@googlegroups.com

Please make a small runnable sample that demonstrates the problem.
http://wiki.wxpython.org/MakingSampleApps

--
Robin Dunn
Software Craftsman
http://wxPython.org

Josh English

unread,
Jan 5, 2010, 11:46:33 PM1/5/10
to wxpytho...@googlegroups.com
I've been thinking about this and thought I'd give it a try. It is
possible to bind wx.EVT_CONTEXT_MENU to a list box. I can't get it to
help.

I can get the position of the event in the ListBox, but the
ListBox.HitTest() method isn't available in MSW (according to my
docs), so the position is useless on my system.

Short of binding the regular EVT_LISTBOX to create the context menu, I
can't find a way to make the context menu work.

Josh

Michael Moriarity

unread,
Jan 6, 2010, 10:37:29 AM1/6/10
to wxpytho...@googlegroups.com


On Tue, Jan 5, 2010 at 11:46 PM, Josh English <joshua.r...@gmail.com> wrote:
I've been thinking about this and thought I'd give it a try. It is
possible to bind wx.EVT_CONTEXT_MENU to a list box. I can't get it to
help.

I can get the position of the event in the ListBox, but the
ListBox.HitTest() method isn't available in MSW (according to my
docs), so the position is useless on my system.

Short of binding the regular EVT_LISTBOX to create the context menu, I
can't find a way to make the context menu work.


I have attached a quick example which I think does what you want. You need to run it from a shell to see the output. HTH.

--
Best Regards,
Michael Moriarity
TestContextMenu.py

Brian J. Soher

unread,
Jan 6, 2010, 11:27:08 AM1/6/10
to wxpytho...@googlegroups.com

Hi,

Windows XP (64-bit), Python 2.5.4, wxPython 2.8.10.1 with AGW from SVN.

I was hoping that someone might have an example of code that creates
widgets dynamically. I've attached a JPEG that shows the widget I'm
trying to recreate. It's busy, but really all I need help with is
the group in the center.

The dynamic widget is the scroll window in the center. In our
program we open a text file where each line of text is used to create
a line in the scrolled window. This line contains a check box, and
two spin controls. If we open a different text file, then the
current set of compound widgets is destroyed and a new set is used to
populate the scrolled window.

Thanks in advance for any advice,

Brian.

dynamic_compound_widget.jpg

C M

unread,
Jan 6, 2010, 1:44:59 PM1/6/10
to wxpytho...@googlegroups.com

Well, maybe don't think of the scrolled window as the "unit" so much; it is
just display space. The "unit" is each "line" in this. So what you might
do is make a class that is a panel, and each panel = one line. That panel
can be its own class, and in the class you would have everything you need.
The class will take as its arguments the data necessary to fill in the fields
(the metabolite names and any default starting values).

One you have this template for a "line", you can do something like:
(where self = your scrolledpanel display area, and assume somewhere
int his .py file or imported you have a PanelLine class that creates the
line you want and knows what to do with initial values data)

for line_of_text in myTextFile:
line = PanelLine(self, -1. initial_values_needed)
self.myBoxSizer.AddWindow(line, 0, border=0, flag=0)

self.Layout()

This might also be done with a wxGrid, wxList, ObjectListView, or
UltimateListCtrl or some other such widget.

Che

Robin Dunn

unread,
Jan 6, 2010, 2:09:51 PM1/6/10
to wxpytho...@googlegroups.com
On 1/5/10 8:46 PM, Josh English wrote:
> I've been thinking about this and thought I'd give it a try. It is
> possible to bind wx.EVT_CONTEXT_MENU to a list box. I can't get it to
> help.
>
> I can get the position of the event in the ListBox, but the
> ListBox.HitTest() method isn't available in MSW (according to my
> docs), so the position is useless on my system.

Try it. I see that there is an implementation for it so it may be that
the docs are wrong and it will work fine.

Christopher Barker

unread,
Jan 7, 2010, 3:15:47 PM1/7/10
to wxpytho...@googlegroups.com
Brian J. Soher wrote:
> I was hoping that someone might have an example of code that creates
> widgets dynamically. I've attached a JPEG that shows the widget I'm
> trying to recreate. It's busy, but really all I need help with is the
> group in the center.

there really isn't that much to it -- Python being a dynamic language,
it's pretty much the same, static or dynamic.

I'd create a compound widget class:

class ChemWidget(wx.Panel):
def __init__(self, parent, chem_name)
...
create your checkbox, labels, etc here (laid out with sizers)

Now when you read the file, in a custom ScrolledPanel:

sizer = wx.BoxSizer(wx.VERTICAL)
chem_widgets = []
# you could also use a dict here, if you want to be able to find
# particular ones more easily.
for chem in chem_list:
cm = ChemWidget(self, chem)
chem_widgets.append(cm)
sizer.Add(cm, 0, wx.CENTER_HORIZONTAL)

I hope that gives you the idea.

-Chris


--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Reply all
Reply to author
Forward
0 new messages