wxPython Frame禁用/启用?

问题描述 投票:5回答:3

我创建了一个wx.Frame(我们称它为mainFrame)。该框架上包含一个按钮,单击该按钮后,将创建一个新框架(我们称其为childFrame)。

我想知道如何在创建childFrame时禁用mainFrame并在销毁/关闭childFrame时再次启用mainFrame?

Regars,

wxpython
3个回答
9
投票

也许您想要这样的东西:


import wx

class MainFrame(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None, wx.NewId(), "Main") 
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.button = wx.Button(self, wx.NewId(), "Open a child")
        self.sizer.Add(self.button, proportion=0, border=2, flag=wx.ALL)
        self.SetSizer(self.sizer)
        self.button.Bind(wx.EVT_BUTTON, self.on_button)

        self.Layout()

    def on_button(self, evt):
        frame = ChildFrame(self)
        frame.Show(True)
        frame.MakeModal(True)

class ChildFrame(wx.Frame): 
    def __init__(self, parent): 
        wx.Frame.__init__(self, parent, wx.NewId(), "Child")
        self.Bind(wx.EVT_CLOSE, self.on_close)

    def on_close(self, evt):
        self.MakeModal(False)
        evt.Skip()

class MyApp(wx.App):
    def OnInit(self):
        frame = MainFrame()
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop()    

2
投票

也许您不需要其他框架,但需要一个模式对话框,例如

import wx

app = wx.PySimpleApp()
mainFrame = wx.Frame(None, title="Click inside me")
def onMouseUp(event):
    dlg = wx.Dialog(mainFrame,title="I am modal, close me first to get to main frame")
    dlg.ShowModal()

mainFrame.Bind(wx.EVT_LEFT_UP, onMouseUp)
mainFrame.Show()
app.SetTopWindow(mainFrame)
app.MainLoop()

0
投票

看起来wx.Frame不再具有MakeModal方法...https://wxpython.org/Phoenix/docs/html/MigrationGuide.html#makemodal

因此您可以将显示的摘录(通过链接)实现到您的子框架中,并与@Alex接受的答案结合使用。

© www.soinside.com 2019 - 2024. All rights reserved.