Menu.Append/AppendSubMenu question

65 views
Skip to first unread message

raf

unread,
Mar 4, 2025, 11:22:27 PM3/4/25
to wx-u...@googlegroups.com
Hi,

I've just started converting a
python-2.7/wxpython-2.8.12.1 app to
python-3.12/wxpython-4.2.2.

The first API change that I've run into is
Menu.Append(). It looks like it used to have
parameters: id, text, help (at least they were the ones
I used - there are others). And the names have changed
to: id, item, helpString.

That's fine. but when I look at:

https://docs.wxpython.org/wx.Menu.html#wx.Menu.Append

It says:

Deprecated This function is deprecated, use AppendSubMenu instead.

AppendSubMenu seems like a unfortunate name for a
function that adds a menuitem with no submenu, but I
assume I can just pass it submenu=None and that'll be
OK (although the docs are very brief and don't state
whether this will be OK or not)

But more importantly, AppendSubMenu doesn't take an id
parameter.

How do I pass it the Id? I also at some point saw
deprecation warnings for NewId(). And looking into it,
it seems that I need to replace all my uses of NewId()
with NewIdRef(), but if I have to use AppendSubMenu(),
which doesn't accept an id parameter, what am I
supposed to do?

I do reuse ids when the same functionality appears in
multiple places within the menu.

Thanks for any advice you might have.

cheers,
raf

Andrea Gavana

unread,
Mar 5, 2025, 12:27:07 AM3/5/25
to wx-u...@googlegroups.com
The documentation is messed up, at least visually. As far as I know, Append() is deprecated only if you want to append a submenu to a menu - for which you should use AppendSubMenu(). For a standard wx.MenuItem it should work without problems and without DeprecationWarnings.

Andrea.


--
Please read https://www.wxwidgets.org/support/mlhowto.htm before posting.
---
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/wx-users/Z8fRdHoVyj0oGRC6%40raf.org.

Vadim Zeitlin

unread,
Mar 5, 2025, 11:25:06 AM3/5/25
to wx-u...@googlegroups.com
On Wed, 5 Mar 2025 15:22:12 +1100 'raf' via wx-users wrote:

r> I've just started converting a
r> python-2.7/wxpython-2.8.12.1 app to
r> python-3.12/wxpython-4.2.2.
r>
r> The first API change that I've run into is
r> Menu.Append().

There has been no changes to wxMenu::Append() in C++, so I'm not sure what
the real problem is. How do you call it exactly and what is the problem
you're seeing?

r> It looks like it used to have
r> parameters: id, text, help (at least they were the ones
r> I used - there are others). And the names have changed
r> to: id, item, helpString.

Other than the change of parameter name, this is still the same function
doing exactly the same thing as before.

r> That's fine. but when I look at:
r>
r> https://docs.wxpython.org/wx.Menu.html#wx.Menu.Append
r>
r> It says:
r>
r> Deprecated This function is deprecated, use AppendSubMenu instead.

Only the overload (in C++ sense) taking wxMenu submenu is deprecated.

r> But more importantly, AppendSubMenu doesn't take an id
r> parameter.

Because submenus don't have IDs.

r> How do I pass it the Id? I also at some point saw
r> deprecation warnings for NewId().

This function is not formally deprecated at C++ level, but it's indeed not
recommended to use it because it allocates ID from 2 disjoint ranges which
can be unexpected. wxIdManager::ReserveId() can be used instead.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
https://www.tt-solutions.com/

raf

unread,
Mar 5, 2025, 9:34:56 PM3/5/25
to wx-u...@googlegroups.com
Thanks. That's good to hear.

It looks like I am currently using AddMenu() to add items with submenus.
That doesn't seem to exist anymore, but it also took an id parameter that
I'm using, and caching for multiple instances. Maybe it doesn't
matter. It's been many years since I had anything to do with this code,
but I might have been caching ids to prevent wasting them. It was probably
allocating new ids everytime the popup menu was dynamically generated
which would happen many times. Perhaps using AppendSubMenu with no ids
will just be better. But I'll get to that soon.

Thanks again.

cheers,
raf


raf

unread,
Mar 5, 2025, 9:38:50 PM3/5/25
to wx-u...@googlegroups.com
Thanks.

> r> But more importantly, AppendSubMenu doesn't take an id
> r> parameter.
>
> Because submenus don't have IDs.

Ah, they used to. Thanks.

> r> How do I pass it the Id? I also at some point saw
> r> deprecation warnings for NewId().
>
> This function is not formally deprecated at C++ level, but it's indeed not
> recommended to use it because it allocates ID from 2 disjoint ranges which
> can be unexpected. wxIdManager::ReserveId() can be used instead.

The documentation says that NewIdRef() is a replacement.
Is that corredt?

> Regards,
> VZ
>
> --
> TT-Solutions: wxWidgets consultancy and technical support
> https://www.tt-solutions.com/

Many thanks.

cheers,
raf

Vadim Zeitlin

unread,
Mar 6, 2025, 10:25:38 AM3/6/25
to wx-u...@googlegroups.com
On Thu, 6 Mar 2025 13:38:22 +1100 'raf' via wx-users wrote:

r> > r> How do I pass it the Id? I also at some point saw
r> > r> deprecation warnings for NewId().
r> >
r> > This function is not formally deprecated at C++ level, but it's indeed not
r> > recommended to use it because it allocates ID from 2 disjoint ranges which
r> > can be unexpected. wxIdManager::ReserveId() can be used instead.
r>
r> The documentation says that NewIdRef() is a replacement.
r> Is that corredt?

This must be a Python-specific function because there is nothing like this
in the C++ API, so I don't know, sorry.

In any case, you can continue using NewId() or, alternatively, create a
menu item with wxID_ANY and then ask for the ID allocated to it.

Scott Talbert

unread,
Mar 6, 2025, 10:37:56 AM3/6/25
to wx-u...@googlegroups.com
On Thu, 6 Mar 2025, Vadim Zeitlin wrote:

> On Thu, 6 Mar 2025 13:38:22 +1100 'raf' via wx-users wrote:
>
> r> > r> How do I pass it the Id? I also at some point saw
> r> > r> deprecation warnings for NewId().
> r> >
> r> > This function is not formally deprecated at C++ level, but it's indeed not
> r> > recommended to use it because it allocates ID from 2 disjoint ranges which
> r> > can be unexpected. wxIdManager::ReserveId() can be used instead.
> r>
> r> The documentation says that NewIdRef() is a replacement.
> r> Is that corredt?
>
> This must be a Python-specific function because there is nothing like this
> in the C++ API, so I don't know, sorry.
>
> In any case, you can continue using NewId() or, alternatively, create a
> menu item with wxID_ANY and then ask for the ID allocated to it.

wx.NewIdRef() in wxPython basically is a wrapper for calling
wxIdManager::ReserveId() and then keeping that ID "alive" while it is
still referenced in Python:

https://github.com/wxWidgets/Phoenix/blob/f49f0b446da60f3f5fe88c54fa9c0c171af76681/etg/windowid.py#L99

Regards,
Scott

raf

unread,
Mar 6, 2025, 7:46:13 PM3/6/25
to wx-u...@googlegroups.com
On Thu, Mar 06, 2025 at 04:25:32PM +0100, Vadim Zeitlin <va...@wxwidgets.org> wrote:

> On Thu, 6 Mar 2025 13:38:22 +1100 'raf' via wx-users wrote:
>
> r> > r> How do I pass it the Id? I also at some point saw
> r> > r> deprecation warnings for NewId().
> r> >
> r> > This function is not formally deprecated at C++ level, but it's indeed not
> r> > recommended to use it because it allocates ID from 2 disjoint ranges which
> r> > can be unexpected. wxIdManager::ReserveId() can be used instead.
> r>
> r> The documentation says that NewIdRef() is a replacement.
> r> Is that correct?
>
> This must be a Python-specific function because there is nothing like this
> in the C++ API, so I don't know, sorry.
>
> In any case, you can continue using NewId() or, alternatively, create a
> menu item with wxID_ANY and then ask for the ID allocated to it.
>
> Regards,
> VZ
>
> --
> TT-Solutions: wxWidgets consultancy and technical support
> https://www.tt-solutions.com/

Thanks.

cheers,
raf

raf

unread,
Mar 6, 2025, 7:47:00 PM3/6/25
to wx-u...@googlegroups.com
Thanks.

cheers,
raf

Reply all
Reply to author
Forward
0 new messages