Search code examples
pythonwxpythonwxwidgetswxformbuilder

How to apply MakeModal() method on frame with child dialog


I am using wxPython Classic on Python 27. I have my files structure as follow (see codes snippet below):-

frame.py = this file contains two classes for frame and dialog (MyFrame1 and MyDialog1 respectively)

main.py = this file import the classes above and also contains two sub-classes based on the above (MyFrame2 and MyDialog2 respectively).

Now keeping the file structure as it is above, how do I make the Frame window inactive when the dialog window is opened?

How can the MakeModal() method be properly applied on the file arrangement/structure above? All the examples I found so far make uses of two frames instead of a frame and a dialog.

frame.py

import wx

# ************ FRAME 1 ************ #
# ************ FRAME 1 ************ #
class MyFrame1 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer = wx.BoxSizer( wx.VERTICAL )

        self.child_button = wx.Button( self, wx.ID_ANY, u"Child Frame", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer.Add( self.child_button, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )


        self.SetSizer( bSizer )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.child_button.Bind( wx.EVT_BUTTON, self.On_child_button )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def On_child_button( self, event ):
        event.Skip()



# ************ DIALOG 1 ************ #
# ************ DIALOG 1 ************ #
class MyDialog1 ( wx.Dialog ):

    def __init__( self, parent ):
        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 300,200 ), style = wx.DEFAULT_DIALOG_STYLE )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )


        self.Centre( wx.BOTH )

        # Connect Events
        self.Bind( wx.EVT_CLOSE, self.onClose )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def onClose( self, event ):
        event.Skip()

main.py

import wx
from frame import MyFrame1, MyDialog1


class MyFrame2(MyFrame1):
    def __init__(self, parent):
        MyFrame1.__init__ (self, parent)

    def On_child_button( self, event ):
        MyDialog2(None).Show()


class MyDialog2(MyDialog1):
    def __init__(self, parent):
        MyDialog1.__init__ (self, parent)

    def onClose(self, event):
        self.Destroy()


app = wx.App(0)
MyFrame2(None).Show()
app.MainLoop()

Solution

  • You shouldn't need to use MakeModal on the frame, as dialogs already have modality functionality built-in. Just call the dialog's ShowModal method instead of the Show method. ShowModal will create a nested event loop so ShowModal will not return until the dialog is finished, and input to the other windows in the application will be blocked. A typical usage pattern looks like this:

    with MyDialog(self, foo, bar) as dlg:
        if dlg.ShowModal() == wx.ID_OK:
            # do something with dlg values