wxPython:为什么无模式、非模式对话框停留在父 window 之上?

wxPython: Why does modeless, non modal dialog stay on top of parent window?

我想打开一个对话框无模式 Show()。打开后我可以点击父 window 这样对话框就成功打开了。 我遇到的问题是无模式对话框始终位于父对话框 window 之上。我希望当我点击它时父级在顶部。

我是否为对话框提供父级 window 并不重要。我测试 wx.STAY_ON_TOP 样式,该样式对于对话框是 False

使用下面的简单代码(从 https://www.tutorialspoint.com/wxpython/wx_dialog_class.htm 复制和修改)我遇到了同样的问题。

我怎样才能得到一个可以落后于父级的无模式对话框window?

import wx

# -----------------------------------------------------------------------------------
class MyDialog(wx.Dialog):
    def __init__(self, parent, title):
        super(MyDialog, self).__init__(parent, title=title, size=(250, 150))
        panel = wx.Panel(self)
        self.btn = wx.Button(panel, wx.ID_OK, label="ok", size=(50, 20), pos=(75, 50))

        style = self.GetWindowStyle()
        if style & wx.STAY_ON_TOP:
            print('STAY_ON_TOP = True')
        else:
            print('STAY_ON_TOP = False')

# -----------------------------------------------------------------------------------
class Mywin(wx.Frame):

    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title=title, size=(300, 300))
        self.InitUI()

    def InitUI(self):
        panel = wx.Panel(self)
        btn1 = wx.Button(panel, label="Modal Dialog", pos=(20, 10))
        btn2 = wx.Button(panel, label="Modeless Dialog Parent", pos=(20, 40))
        btn3 = wx.Button(panel, label="Modeless Dialog Parentless", pos=(20, 70))

        btn1.Bind(wx.EVT_BUTTON, self.OnModal)
        btn2.Bind(wx.EVT_BUTTON, self.OnModelessParent)
        btn3.Bind(wx.EVT_BUTTON, self.OnModelessParentless)
        self.Show(True)

    def OnModal(self, event):
        MyDialog(self, "Dialog").ShowModal()

    def OnModelessParent(self, event):
        dlg = MyDialog(self, "Dialog").Show()

    def OnModelessParentless(self, event):
        dlg = MyDialog(None, "Dialog").Show()

# -----------------------------------------------------------------------------------
ex = wx.App()
Mywin(None, 'Modal / Modeless')
ex.MainLoop()

将样式 wx.DIALOG_NO_PARENT 添加到对话框的构造函数中可以使对话框浮动在框架后面。例如:

import wx


# -----------------------------------------------------------------------------------
class MyDialog(wx.Dialog):
    def __init__(self, parent, title):
        super(MyDialog, self).__init__(parent, title=title, size=(250, 150),
                                       style=wx.DEFAULT_DIALOG_STYLE | wx.DIALOG_NO_PARENT)
        panel = wx.Panel(self)
        self.btn = wx.Button(panel, wx.ID_OK, label="ok", size=(50, 20), pos=(75, 50))

        style = self.GetWindowStyle()
        if style & wx.STAY_ON_TOP:
            print('STAY_ON_TOP = True')
        else:
            print('STAY_ON_TOP = False')


# -----------------------------------------------------------------------------------
class Mywin(wx.Frame):

    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title=title, size=(300, 300))
        self.InitUI()

    def InitUI(self):
        panel = wx.Panel(self)
        btn1 = wx.Button(panel, label="Modal Dialog", pos=(20, 10))
        btn2 = wx.Button(panel, label="Modeless Dialog Parent", pos=(20, 40))
        btn3 = wx.Button(panel, label="Modeless Dialog Parentless", pos=(20, 70))

        btn1.Bind(wx.EVT_BUTTON, self.OnModal)
        btn2.Bind(wx.EVT_BUTTON, self.OnModelessParent)
        btn3.Bind(wx.EVT_BUTTON, self.OnModelessParentless)
        self.Show(True)

    def OnModal(self, event):
        MyDialog(self, "Dialog").ShowModal()

    def OnModelessParent(self, event):
        MyDialog(self, "Dialog").Show()

    def OnModelessParentless(self, event):
        MyDialog(None, "Dialog").Show()


# -----------------------------------------------------------------------------------
ex = wx.App()
Mywin(None, 'Modal / Modeless')
ex.MainLoop()

我删除了您的 2 个无模式事件处理程序上的变量赋值 dlg =,因为 Show() returns True 不是对话框实例。

还应注意,对话框不会自动销毁,您必须在完成后手动调用 Destroy() 以释放它们的内存。