如何在wxPython(Phoenix)中创建模态对话框?

3

wxPython 2曾经存在

self.MakeModal(True)

但是在Phoenix中不同。

如何展示Modal?

我不想使用 wx.Dialog,因为我需要添加状态栏。

2个回答

4
我使用的一个很好的解决方法是将样式标志 wx.FRAME_FLOAT_ON_PARENT 添加到窗口,并通过frame.GetParent().Disable()禁用父窗口。然后绑定模态窗口到EVT_CLOSE,并通过frame.GetParent().Enable()重新启用父窗口。 ------ 更新 ----- Robin Dunn的建议是一种更适合将窗口设置为模态的方法。从wx phoenix迁移指南中,将以下代码添加到您的Frame类中。
def MakeModal(self, modal=True):
    if modal and not hasattr(self, '_disabler'):
        self._disabler = wx.WindowDisabler(self)
    if not modal and hasattr(self, '_disabler'):
        del self._disabler

另一种选择请参见Phoenix迁移指南中的MakeModal部分:https://wxpython.org/Phoenix/docs/html/MigrationGuide.html#makemodal - RobinDunn

0

从稍微不同的角度来看,我发现我需要等待一个编辑数据库项的类,以便重新阅读和呈现更改后的数据,但我希望该类在所有其他情况下都能正常运行。

因此,它应该是“按请求”模态的。

我发现最简单的方法是向类传递一个参数,如果为True,它将立即禁用父级。然后在退出时,它将启用父级。

同时,调用例程将循环执行self.IsEnabled()

例如:

调用例程:

    OnNote(self, audio, "", modal=True)
    while not self.IsEnabled():
        wx.GetApp().Yield()
        time.sleep(0.5)

调用的类:

class OnNote(wx.Frame):
    def __init__(self, parent, filename, database, modal=False):
        wx.Frame.__init__(self, parent, wx.ID_ANY, "Footswitch Notes")
        self.SetWindowStyle(wx.STAY_ON_TOP)
        self.parent = parent
        self.modal = modal
        if self.modal:
            self.parent.Disable()

........

退出调用类:

    if self.modal:
        self.parent.Enable()
    self.Destroy()

这太棒了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接