I believe I have found a bug in wxpython (or wxWidgets). I am using WIndows 11, wxpython 4.2.1 wxWidgets 3.2.2.1, Python 3.11.5. Take this simple program (simplified from Getting Started - wxPyWiki):
import wx
class MainWindow(wx.Frame):
def init(self, parent, title):
wx.Frame.init(self, parent, title=title, size=(400,400))
wx.TextCtrl(self, style=wx.TE_MULTILINE)
filemenu = wx.Menu()
filemenu.Append(wx.ID_EXIT, “E&xit”)
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File")
self.SetMenuBar(menuBar)
self.Show(True)
app = wx.App(False)
frame = MainWindow(None, “Sample editor”)
app.MainLoop()
Hit Alt-F then Esc: the menu does disappear but the TextCtrl does not properly regain the focus: the first charer typed is ignored except there is a bleep.
The only fix I have found that works is:
TextCtrl traps EVT_MENU_CLOSE
Save mouse position
Simulate a click with wx.UIActionSimulator
Restore mouse position
If the menu is used to say pop up a dialog, do wx.Yield() before popping it up (otherwise there is a bleep because the dialog sees a click outside itself).
Rob Cliffe
Sorry step 3 should read
Simulate a mouse click on the frame title (or anywhere on the app but not on the TextCtrl or one of its scrollbars)
And “charer” was a typo for “character”.
In your post, if you put three backtick characters ` before the first line of code and three backtick characters after the last line of code, then the forum software won’t reformat it (which loses the double underscores and indentation and changes the quotes into invalid characters).
I ran that code using wxPython 4.2.1 gtk3 (phoenix) wxWidgets 3.2.4 + Python 3.12.3 + Linux Mint 22.
After pressing Alt-F then Esc the TextCtrl did regain the focus and no typed characters were lost. It appears to be working as expected on Linux.