2014-02-12 23 views
1

推導出其他一些主題指出了一個通用的MessageBox,wx.MessageDialog並不很多API函數,如Destroy()等的外部調用因此,有必要建立源自一個wx.GenericMessageBox迴應wx.Dialog。 這就是:從的wx.Dialog

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

不像wx.lib.agw.genericmessagedialog,這個人的目標是到最接近期待能夠原生操作系統的外觀(這裏的Windows外觀)。 [genericmessagedialog具有的按鈕,這是不是像Windows'本地外觀圖片]

它是如何將有可能提高,從而該對話框中,如果需要StaticText兩行的大小會自動增加?

此外,確定按鈕(x,y)定位是否正常並居中在我機器的灰色面板上,但在其他平臺上是否一樣?

(我覺得這樣的一個片段可能是社區有用。)

回答

1

要管理的佈局,最好是使用,而不是位置。

你可以有下面的代碼試試:

使用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

+0

非常感謝。是否可以爲'wx.StaticText'設置最大寬度,以便自動分割成幾行文本的段落太長? – Basj

+1

@Basj是的,嘗試** self.m_staticText.Wrap(SIZE)** –