Left-Most Button in StdDialogButtonSizer Always The Default

When I add buttons to StdDialogButtonSizer, the left-most displayed button is always the default. On Linux (TuxedoOS, essentially Debian testing and KDE) with Python 3.14.7 and wxPython 4.3.2

Here is a test program:

import wx

class TestDialog(wx.Dialog):
    def __init__(self):
        super(TestDialog, self).__init__(
            None, title="Test Dialog")
        text = wx.StaticText(self, label="Some random text")

        ok = wx.Button(self, id = wx.ID_OK)
        ok.SetDefault()
        cancel = wx.Button(self, id = wx.ID_CANCEL)
        help = wx.Button(self, wx.ID_HELP)
        
        button_sizer = wx.StdDialogButtonSizer()
        button_sizer.AddButton(ok)
        button_sizer.AddButton(cancel)
        button_sizer.AddButton(help)
        button_sizer.SetAffirmativeButton(ok)
        button_sizer.Realize()

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(text, flag = wx.ALL, border = 5)
        sizer.Add(button_sizer, flag = wx.LEFT | wx.RIGHT | wx.BOTTOM,
                  border = 10)

        self.SetSizer(sizer)

if __name__ == '__main__':
    app = wx.App()
    dialog = TestDialog()
    dialog.ShowModal()

Whether the code ok.SetDefault() or button_sizer.SetAffirmativeButton(ok) is included or not, the left-most button (the help button on KDE) is always the default.
How can I set the OK button, which on my system is the right-most, as the default?

I tested your code using wxPython 4.3.1 gtk3 (phoenix) wxWidgets 3.3.3 + Python 3.12.3 + Linux Mint 22.3 and got the same result as you.

I did some experiments and found that if I replaced the StaticText with an interactive control such as a ListBox, TextCtrl, or CheckBox, then the button on the right was shown as the default.

Why it behaves like that I don’t know.

Thank you RichardT

Interesting. I changed the StaticText to a TextCtrl, and now the TextCtrl is the selected. Pressing the Enter key indicates that the OK button is the default.

I have created a bug report (#2994) for this.

As a workaround for your original example, try adding ok.SetFocus() to the end of the __init__ method…

As a workaround for your original example, try adding ok.SetFocus() to the end of the __init__ method…

Yes, that helps as well.