How do I detect if a wx.Menu has been destroyed

Question:

I’m porting an application from:

  • Python 2.5.4
  • 2.8.10.1 (msw-unicode)

to:

  • Python 3.7.9
  • 4.1.1 msw (phoenix) wxWidgets 3.1.5.

The following snippet gives different results. In particular it seems that previously you were able to detect if a wx.Menu had been deleted by just doing "if widget".

If that doesn’t work anymore, how can I check whether a wx.Menu has been deleted?

import wx

app = wx.App(redirect=False)
menu = wx.Menu()
menu.Destroy()
print(bool(menu))
  • 2.8.10.1 = False
  • 4.1.1 = True
Asked By: Anton Lahti

||

Answers:

I can’t check how it used to work as I have no access to a legacy system.

However you can write a simple function to check whether or not the item is deleted by looking at its Window property. Just pass the reference to the menu into the function check_menu

def __init__(self, *args, **kwargs):
    super().__init__(None, *args, **kwargs)
    menu = wx.Menu()
    self.check_menu(menu)
    menu.Destroy()
    self.check_menu(menu)


@staticmethod
def check_menu(menu):
    try:
        menu.Window
        print('active')
    except RuntimeError:
        print('deleted')

Gives the results

active

deleted

Answered By: Psionman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.