Why does CenterOnParent not work consistently?

I find that .CenterOnParent() does not always work as I would expect. Here is a code snippet:

dlg = wx.MessageDialog(self.GetTopLevelParent(), msg, caption=header, style=wx.YES_NO)
dlg.CenterOnParent()
try:
    result = dlg.ShowModal()
finally:
    dlg.Destroy()

In this case self is a already a subclass of wx.Frame so the self.GetTopLevelParent() is unneeded, but does not make a difference. What I am seeing is that Message dialog is placed on the same screen as the existing TopLevel, but not on top of it. It seems in some places in my code window.CenterOnParent() does its job but not always. FWIW, at the moment I am using wx 4.1.1 w/Python 3.9.1 on a Mac.

Brian

Does style wx.CENTER work?

See also here: wx.MessageDialog.Center causes segmentation fault on 4.1.1

and here: https://github.com/wxWidgets/wxWidgets/issues/19038

Your link (wx.MessageDialog.Center causes segmentation fault on 4.1.1) has Robin’s answer: wx.MessageDialog here is a native, rather than wx-generated widget, so wx can’t move it. If I want to center the frame, I need to create my own subclassed from wx.Dialog.

That explains why sometimes my dialogs get centered but not always. Thanks!

I think this is the actual implementation: https://github.com/wxWidgets/wxWidgets/blob/6268b5c619ac7bd7479c0912275bce89d67d1f23/src/osx/cocoa/msgdlg.mm
It just calls CFUserNotificationDisplayAlert. I don’t know how that behaves, as I’m mostly on Windows.

You could try GenericMessageDialog.