How to make a wxFrame behave like a modal wxDialog object

后端 未结 4 1906
萌比男神i
萌比男神i 2021-01-12 04:51

Is is possible to make a wxFrame object behave like a modal dialog box in that the window creating the wxFrame object stops execution until the wxFrame object exits?

4条回答
  •  渐次进展
    2021-01-12 05:13

    This took me an annoying amount of time to figure out but here is a working example that grew out of Anurag's example:

    import wx
    
    class ChildFrame(wx.Frame):
        ''' ChildFrame launched from MainFrame '''
        def __init__(self, parent, id):
            wx.Frame.__init__(self, parent, -1,
                              title=self.__class__.__name__,
                              size=(300,150))
    
            panel = wx.Panel(self, -1)
            closeButton = wx.Button(panel, label="Close Me")
    
            self.Bind(wx.EVT_BUTTON, self.__onClose, id=closeButton.GetId())
            self.Bind(wx.EVT_CLOSE, self.__onClose) # (Allows frame's title-bar close to work)
    
            self.CenterOnParent()
            self.GetParent().Enable(False)
            self.Show(True)
    
            self.__eventLoop = wx.EventLoop()
            self.__eventLoop.Run()
    
        def __onClose(self, event):
            self.GetParent().Enable(True)
            self.__eventLoop.Exit()
            self.Destroy()
    
    class MainFrame(wx.Frame):
        ''' Launches ChildFrame when button is clicked. '''
        def __init__(self, parent, id):
            wx.Frame.__init__(self, parent, id,
                              title=self.__class__.__name__,
                              size=(400, 300))
    
            panel = wx.Panel(self, -1)
            launchButton = wx.Button(panel, label="launch modal window")
    
            self.Bind(wx.EVT_BUTTON, self.__onClick, id=launchButton.GetId())
    
            self.Centre()
            self.Show(True)
    
        def __onClick(self, event):
            dialog = ChildFrame(self, -1)
            print "I am printed by MainFrame and get printed after ChildFrame is closed"
    
    if __name__ == '__main__':
        app = wx.App()    
        frame = MainFrame(None, -1)
        frame.Show()
        app.MainLoop()
    

提交回复
热议问题