Question

As pointed out in some other topics, wx.MessageDialog doesn't respond to many API functions, such as an external call from Destroy(), etc. So there is a need for building a wx.GenericMessageBox derived from wx.Dialog. Here it is :

enter image description here

import  wx

class GenericMessageBox(wx.Dialog):
    def __init__(self, parent, text, title = ''):
        wx.Dialog.__init__(self, parent, -1, title = title, size = (360,120), style = wx.DEFAULT_DIALOG_STYLE)
        panel = wx.Panel(self, wx.ID_ANY, size = (360, 50), pos = (0,0))
        panel.SetBackgroundColour('#FFFFFF')
        label = wx.StaticText(panel, -1, text, pos = (50,20))        
        panel2 = wx.Panel(self, wx.ID_ANY, size = (360, 40), pos = (0, 50))
        btn = wx.Button(panel2, wx.ID_OK, pos = (250,7))
        self.ShowModal()


app = wx.App()
frame = wx.Frame(None, 0, 'Test')
frame.Show()
GenericMessageBox(frame, 'This is a message box that is derived from wx.Dialog. You can Destroy() it from anywhere in the code.', 'Test')
app.MainLoop()

Unlike wx.lib.agw.genericmessagedialog, this one's goal is to have the closest look possible to native OS look (here Windows look). [genericmessagedialog has pictures in the buttons, which is not like Windows' native look]

How would it be possible to improve this dialog in order that it its size is automatically increased if StaticText needs two lines?

Moreover the OK button (x,y) positionning is okay and centered on the grey panel on my machine, but would it be the same on other platforms?

(I think such a snippet could be useful for community.)

Was it helpful?

Solution

To manage the Layout, it is better to use Sizer instead of position.

You can have a try with following code:

use self.setMessageLine(Num) to set the output to see the difference.

    import wx

    class MyDialog ( 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 )

            #set the minimum Size of the frame to Fit()
            self.SetSizeHintsSz( wx.Size( 300,-1 ), wx.DefaultSize )

            fgSizer = wx.FlexGridSizer( 2, 1, 0, 0 )
            fgSizer.AddGrowableCol( 0 )
            fgSizer.AddGrowableRow( 0 )
            fgSizer.SetFlexibleDirection( wx.BOTH )
            fgSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_ALL )

            self.m_panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SIMPLE_BORDER|wx.TAB_TRAVERSAL )
            gSizer = wx.GridSizer( 1, 1, 0, 0 )

            self.m_staticText = wx.StaticText( self.m_panel, wx.ID_ANY, u"test", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText.Wrap( 10 )
            gSizer.Add( self.m_staticText, 0, wx.ALIGN_CENTER|wx.ALL, 5 )


            self.m_panel.SetSizer( gSizer )
            self.m_panel.Layout()
            gSizer.Fit( self.m_panel )
            fgSizer.Add( self.m_panel, 1, wx.EXPAND |wx.ALL, 5 )

            m_sdbSizer = wx.StdDialogButtonSizer()
            self.m_sdbSizerOK = wx.Button( self, wx.ID_OK )
            m_sdbSizer.AddButton( self.m_sdbSizerOK )
            self.m_sdbSizerCancel = wx.Button( self, wx.ID_CANCEL )
            m_sdbSizer.AddButton( self.m_sdbSizerCancel )
            m_sdbSizer.Realize()

            fgSizer.Add( m_sdbSizer, 1, wx.ALL|wx.EXPAND, 5 )
            self.SetSizer( fgSizer )
            self.Layout()
            self.Centre( wx.BOTH )
            self.setMessageLine(10)

        def setMessageLine(self, messageLine):
            msg = ""
            for i in range(0, messageLine):
                msg += "Line %d \n" % i

            self.m_staticText.SetLabel(msg)
            self.Fit()

    if __name__ == '__main__':
        app = wx.App()
        dlg = MyDialog(None)
        dlg.ShowModal()
        app.MainLoop()
        pass

enter image description here enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top