How to make a wxFrame behave like a modal wxDialog object

后端 未结 4 1907
萌比男神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:07

    I was also looking from similar solution and have comeup with this solution, create a frame, disable other windows by doing frame.MakeModal() and to stop execution start and event loop after showing frame, and when frame is closed exit the event loop e.g. I here is sample using wxpython but it should be similar in wxwidgets.

    import wx
    
    class ModalFrame(wx.Frame):
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, title=title, style=wx.DEFAULT_FRAME_STYLE|wx.STAY_ON_TOP)
    
            btn = wx.Button(self, label="Close me")
            btn.Bind(wx.EVT_BUTTON, self.onClose)
            self.Bind(wx.EVT_CLOSE, self.onClose) # (Allows main window close to work)
    
        def onClose(self, event):
            self.MakeModal(False) # (Re-enables parent window)
            self.eventLoop.Exit()
            self.Destroy() # (Closes window without recursion errors)
    
        def ShowModal(self):
            self.MakeModal(True) # (Explicit call to MakeModal)
            self.Show()
    
            # now to stop execution start a event loop 
            self.eventLoop = wx.EventLoop()
            self.eventLoop.Run()
    
    
    app = wx.PySimpleApp()
    frame = wx.Frame(None, title="Test Modal Frame")
    btn = wx.Button(frame, label="Open modal frame")
    
    def onclick(event):
        modalFrame = ModalFrame(frame, "Modal Frame")
        modalFrame.ShowModal()
        print "i will get printed after modal close"
    
    btn.Bind(wx.EVT_BUTTON, onclick)
    
    frame.Show()
    app.SetTopWindow(frame)
    app.MainLoop()
    

提交回复
热议问题