Dynamically generate items in wxChoice item

I understand how to use clear and append to dynamically generate items in a
wxChoice item, however I don't see how to catch the event that triggers the
generation of the menu of choices. In Qt, it is possible to create a
listbox and make it the listbox for the combobox. Then one can catch the
show event for the listbox. However, in wxPython, I don't see a way to
assign the thing (a menu?) that contains the list of choices to the choice
item. Nor do I see an event that occurs when that list is generated. I'm
sure I'm missing something obvious as I am just starting to use wxPython,
so I would sure appreciate a pointer from someone with more experience.

···

--
Jeffrey Barish

Jeffrey Barish wrote:

I understand how to use clear and append to dynamically generate items in a
wxChoice item, however I don't see how to catch the event that triggers the
generation of the menu of choices. In Qt, it is possible to create a
listbox and make it the listbox for the combobox. Then one can catch the
show event for the listbox. However, in wxPython, I don't see a way to
assign the thing (a menu?) that contains the list of choices to the choice
item.

Becase the pop-up or drop-down is part of the native widget and it is not individually replaceable.

Nor do I see an event that occurs when that list is generated.

Because there isn't one. Again, it is because it is handled internally to the native widget and is not exposed to wxWidgets. (Although I think there is a notification after the list is shown on at least Windows, but wxWdidgets isn't turning that into an event either.)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Jeffrey Barish wrote:

I understand how to use clear and append to dynamically generate items in
a wxChoice item, however I don't see how to catch the event that triggers
the
generation of the menu of choices. In Qt, it is possible to create a
listbox and make it the listbox for the combobox. Then one can catch the
show event for the listbox. However, in wxPython, I don't see a way to
assign the thing (a menu?) that contains the list of choices to the
choice item.

Becase the pop-up or drop-down is part of the native widget and it is
not individually replaceable.

Nor do I see an event that occurs when that list is generated.

Because there isn't one. Again, it is because it is handled internally
to the native widget and is not exposed to wxWidgets. (Although I think
there is a notification after the list is shown on at least Windows, but
wxWdidgets isn't turning that into an event either.)

I appreciate the information, but I am not clear about where to go from
here. Do I need to create my own widget? Would I do that by modifying the
wxWidgets C++ code, or is there a way in Python? Is there a way to
subclass wxChoice to introduce the functionality I need? Is there an
existing widget (e.g., wxComboBox) that I can configure to look like
wxChoice and that provides the necessary event?

···

--
Jeffrey Barish

Sometimes if you explain what exactly you are trying to accomplish, we might be able to suggest a good approach. I find that the contents of my wx.Choice objects aren't determined at the moment that the user drops the list, but at some prior moment. E.g., the options available in the list might be determined by a radio box selection, so when they make that selection I repopulate the wx.Choice control.

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com

···

On Dec 30, 2005, at 7:10 PM, Jeffrey Barish wrote:

I appreciate the information, but I am not clear about where to go from
here. Do I need to create my own widget? Would I do that by modifying the
wxWidgets C++ code, or is there a way in Python? Is there a way to
subclass wxChoice to introduce the functionality I need? Is there an
existing widget (e.g., wxComboBox) that I can configure to look like
wxChoice and that provides the necessary event?

Ed Leafe wrote:

I appreciate the information, but I am not clear about where to go
from
here. Do I need to create my own widget? Would I do that by
modifying the
wxWidgets C++ code, or is there a way in Python? Is there a way to
subclass wxChoice to introduce the functionality I need? Is there an
existing widget (e.g., wxComboBox) that I can configure to look like
wxChoice and that provides the necessary event?

     Sometimes if you explain what exactly you are trying to
accomplish, we might be able to suggest a good approach. I find that
the contents of my wx.Choice objects aren't determined at the moment
that the user drops the list, but at some prior moment. E.g., the
options available in the list might be determined by a radio box
selection, so when they make that selection I repopulate the
wx.Choice control.

There is a database on another machine. The data is organized in
categories. The contents of the database, including the categories, can
change. When the user activates the choice widget, the list that appears
is supposed to represent the current selection of categories. Once the
user selects the category, I need to display what it is. The GUI runs on a
PDA, where screen real estate is precious. The drop-down list works
perfectly in my PyQt prototype. When the user clicks on the drop-down
list, the program goes out to the database to collect the current list of
categories, which it then uses to populate the list. The current category
selection gets displayed in the text box associated with the widget. I
thought about keeping a local list of the categories and updating it
periodically, but that solution runs the risk that the database changes
between the time of an update and the moment the user activates the widget.

Your asking this question makes me worry that there is no practical solution
in wxPython to this problem. I suppose that there must be the option of
writing a widget in wxWidgets, but my experience in C++ programming never
got much beyond "Hello, world".

···

On Dec 30, 2005, at 7:10 PM, Jeffrey Barish wrote:

--
Jeffrey Barish

There is a database on another machine. The data is organized in
categories. The contents of the database, including the categories, can
change. When the user activates the choice widget, the list that appears
is supposed to represent the current selection of categories. Once the
user selects the category, I need to display what it is. The GUI runs on a
PDA, where screen real estate is precious. The drop-down list works
perfectly in my PyQt prototype. When the user clicks on the drop-down
list, the program goes out to the database to collect the current list of
categories, which it then uses to populate the list. The current category
selection gets displayed in the text box associated with the widget. I
thought about keeping a local list of the categories and updating it
periodically, but that solution runs the risk that the database changes
between the time of an update and the moment the user activates the widget.

     So what it seems you want is a button to initiate the database query, and then display a list based on the returned values. Perhaps PyQt dropdown lists (the more generic term for the wx.Choice control) work the way you describe, but all the ones I have do not, including wx.

     I'd suggest the following: create a wx.ListBox, and keep it invisible. Create a static text control and a button, and when the user clicks the button, run your query and use the results to populate the ListBox. Show the list box, and when the user makes their selection, hide it, and set the static text equal to their selection.

Your asking this question makes me worry that there is no practical solution
in wxPython to this problem. I suppose that there must be the option of
writing a widget in wxWidgets, but my experience in C++ programming never
got much beyond "Hello, world".

     There are solutions, just not with the control you had in mind. It's always better to start with an understanding of the problem before selecting a particular solution.

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com

···

On Dec 30, 2005, at 9:08 PM, Jeffrey Barish wrote:

Ed Leafe wrote:

There is a database on another machine. The data is organized in
categories. The contents of the database, including the categories, can
change. When the user activates the choice widget, the list that appears
is supposed to represent the current selection of categories. Once the
user selects the category, I need to display what it is. The GUI runs on a
PDA, where screen real estate is precious. The drop-down list works
perfectly in my PyQt prototype. When the user clicks on the drop-down
list, the program goes out to the database to collect the current list of
categories, which it then uses to populate the list. The current category
selection gets displayed in the text box associated with the widget. I
thought about keeping a local list of the categories and updating it
periodically, but that solution runs the risk that the database changes
between the time of an update and the moment the user activates the widget.

    So what it seems you want is a button to initiate the database query, and then display a list based on the returned values. Perhaps PyQt dropdown lists (the more generic term for the wx.Choice control) work the way you describe, but all the ones I have do not, including wx.

I don't fully understand this, but .....

PythonCard (which is also a layer over wxPython) does work this way. A "Choice" component in PythonCard *does* get a mouseDown event (and also a gainFocus event), and that handler can then be used to populate the dropdown list before it appears; I often use them this way, for situations like Jeffrey described. A Choice component is, of course, just a wx.Choice control.

Unfortunately, the event handling code in PythonCard is, um, a bit hard to follow (at least for me), so I can't figure out *how* it manages to get the wx event from which to trigger the mouseDown events.

As far as I can see, it basically just does a Bind for the EVT_LEFT_DOWN event - but doing what seems to be the equivalent, I've been unable to get these events in straight wxPython. However, I know that PythonCard doesn't do anything outside wxPython, this must be possible.

Sorry if this just adds confusion .... I'll keep looking at the PythonCard code to see if I can figure out what it's doing.

···

On Dec 30, 2005, at 9:08 PM, Jeffrey Barish wrote:

    I'd suggest the following: create a wx.ListBox, and keep it invisible. Create a static text control and a button, and when the user clicks the button, run your query and use the results to populate the ListBox. Show the list box, and when the user makes their selection, hide it, and set the static text equal to their selection.

Your asking this question makes me worry that there is no practical solution
in wxPython to this problem. I suppose that there must be the option of
writing a widget in wxWidgets, but my experience in C++ programming never
got much beyond "Hello, world".

    There are solutions, just not with the control you had in mind. It's always better to start with an understanding of the problem before selecting a particular solution.

--
Alex Tweedly http://www.tweedly.net

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.9/217 - Release Date: 30/12/2005

Alex Tweedly wrote:

    So what it seems you want is a button to initiate the database query, and then display a list based on the returned values. Perhaps PyQt dropdown lists (the more generic term for the wx.Choice control) work the way you describe, but all the ones I have do not, including wx.

I don't fully understand this, but .....

I still don't fully understand it (event are a bit of a mystery to me .... ) but you can do this in straight wxPython. Attached is a modified version of the "Choice" sample program from the wxPython demo. It has two choice controls - the first one has bindings for EVT_LEFT_DOWN and EVT_SET_FOCUS and modifies the list before display (note that if another control has focus, and you then click on the Choice control, you get both a mouse and then a focus event). The second one has the (previous, unsuccessful) attempt to do the same.

I have no idea why one binding works and the other doesn't - I'd be very grateful if anyone can explain it to me.

AFAIK, these two events cover all ways that the choice control can be invoked, so setting up the list like this will achieve what Jeffrey wanted.

    I'd suggest the following: create a wx.ListBox, and keep it invisible. Create a static text control and a button, and when the user clicks the button, run your query and use the results to populate the ListBox. Show the list box, and when the user makes their selection, hide it, and set the static text equal to their selection.

Your asking this question makes me worry that there is no practical solution
in wxPython to this problem. I suppose that there must be the option of
writing a widget in wxWidgets, but my experience in C++ programming never
got much beyond "Hello, world".

    There are solutions, just not with the control you had in mind. It's always better to start with an understanding of the problem before selecting a particular solution.

I think there is a solution using wx.Choice - and that that is a much simpler method than using button + list.

choice.py (2.29 KB)

···

--
Alex Tweedly http://www.tweedly.net

Yes, you're correct; I've just checked that with a Dabo dropdown list control, and you can indeed populate it in the onMouseLeftDown() event.

     I had just assumed that based on Robin's reply tying the population of the list to the clicking of it was not possible that some other means was requried.

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com

···

On Dec 31, 2005, at 5:31 AM, Alex Tweedly wrote:

PythonCard (which is also a layer over wxPython) does work this way. A "Choice" component in PythonCard *does* get a mouseDown event (and also a gainFocus event), and that handler can then be used to populate the dropdown list before it appears;

Alex Tweedly wrote:

I still don't fully understand it (event are a bit of a mystery to me
.... ) but you can do this in straight wxPython. Attached is a modified
version of the "Choice" sample program from the wxPython demo. It has
two choice controls - the first one has bindings for EVT_LEFT_DOWN and
EVT_SET_FOCUS and modifies the list before display (note that if another
control has focus, and you then click on the Choice control, you get
both a mouse and then a focus event). The second one has the (previous,
unsuccessful) attempt to do the same.

I have no idea why one binding works and the other doesn't - I'd be very
grateful if anyone can explain it to me.

AFAIK, these two events cover all ways that the choice control can be
invoked, so setting up the list like this will achieve what Jeffrey
wanted.

Your suggestion is, indeed, exactly what I want. I would also be interested
in an explanation for why one works but not the other. Events -- and
everything wxPython -- are still even more of a mystery to me as I have
been using wxPython for about 2 hours. Thank you very much for the help.

···

--
Jeffrey Barish

The one that works binds the mouse down event to the choice control.
The one that doesn't work binds the mouse down event to the choice
control's parent.

It seems that some controls don't propagate mouse events to their parent
before they handle it themselves.

- Josiah

···

Jeffrey Barish <jeff_barish@earthlink.net> wrote:

Alex Tweedly wrote:

> I still don't fully understand it (event are a bit of a mystery to me
> .... ) but you can do this in straight wxPython. Attached is a modified
> version of the "Choice" sample program from the wxPython demo. It has
> two choice controls - the first one has bindings for EVT_LEFT_DOWN and
> EVT_SET_FOCUS and modifies the list before display (note that if another
> control has focus, and you then click on the Choice control, you get
> both a mouse and then a focus event). The second one has the (previous,
> unsuccessful) attempt to do the same.
>
> I have no idea why one binding works and the other doesn't - I'd be very
> grateful if anyone can explain it to me.
>
> AFAIK, these two events cover all ways that the choice control can be
> invoked, so setting up the list like this will achieve what Jeffrey
> wanted.

Your suggestion is, indeed, exactly what I want. I would also be interested
in an explanation for why one works but not the other. Events -- and
everything wxPython -- are still even more of a mystery to me as I have
been using wxPython for about 2 hours. Thank you very much for the help.
--
Jeffrey Barish

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Jeffrey Barish wrote:

Your suggestion is, indeed, exactly what I want. I would also be interested
in an explanation for why one works but not the other. Events -- and
everything wxPython -- are still even more of a mystery to me as I have
been using wxPython for about 2 hours. Thank you very much for the help.

If you're just starting out with wxPython, you might want to consider using either Dabo or PythonCard.

Either one has a much gentler learning curve than straight wxPython, but both allow complete access to all the wxPython features "below" them if you need something not currently supported by the higher-level package; even if you eventually decide that you prefer the feel (or "style") of programming directly in wxPython, most of your experience will be relevant.

www.dabodev.com
www.pythoncard.org

···

--
Alex Tweedly http://www.tweedly.net

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.9/217 - Release Date: 30/12/2005