从wx.Dialog派生的通用MessageBox

时间:2014-02-12 15:05:33

标签: python dialog modal-dialog wxpython messagebox

正如其他一些主题所指出的,wx.MessageDialog不响应许多API函数,例如来自Destroy()的外部调用等。因此需要构建{{1}从wx.GenericMessageBox派生而来。 这是:

enter image description here

wx.Dialog

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() 不同,这个目标是让最接近本机操作系统的外观(这里是Windows外观)。 [ wx.lib.agw.genericmessagedialog按钮中有图片,与Windows的原生外观不同]

如果genericmessagedialog需要两行,那么如何改进此对话框的大小会自动增加?

此外OK按钮(x,y)定位正常并且以我机器上的灰色面板为中心,但在其他平台上是否相同?

(我认为这样的片段对社区有用。)

1 个答案:

答案 0 :(得分:1)

要管理布局,最好使用 Sizer 而不是位置。

您可以尝试使用以下代码:

使用self.setMessageLine(Num)设置输出以查看差异。

    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