2013-07-22 71 views
0

我想以固定大小在屏幕中央放置一個視圖,並且一些靜態文本以水平和垂直方向居中顯示。Python/WXWidgets:ST_NO_AUTORESIZE不被認可爲wx.StaticText

到目前爲止,我有以下代碼:

import wx 
class DisplayText(wx.Dialog): 

    def __init__(self, parent, text="", displayMode=0): 

     # Initialize dialog 
     wx.Dialog.__init__(self, parent, size=(480,320), style=(wx.DIALOG_EX_METAL | wx.STAY_ON_TOP)) 

     # Center form 
     self.Center() 
     self.txtField = wx.StaticText(self, label=text, pos=(80,120), size=(320,200), style=wx.ALIGN_CENTRE_HORIZONTAL | wx.ST_NO_AUTORESIZE) 

     self.txtField.SetFont(wx.Font(24, wx.DEFAULT, wx.BOLD, 0))  

app = wx.App(False) 

c = DisplayText(None, text="Now is the time for all good men to come to the aid of their country.") 
c.Show() 
app.MainLoop() 

的目標是居然有文字垂直居中,但現在,我只是想更明確一些靜態文本上的定位框架。

對於短暫的瞬間,文本出現在我放入的位置,但它會立即跳到窗口的上邊界並擴展到最大寬度。 (我故意將寬度和位置設置得很低,以便能夠看到這種行爲是否正在發生。)

如果我使用wx.Dialog或wx.Frame,則無關緊要。

正如你所看到的,我確實定義了NO_AUTORESIZE標誌,但是這並沒有被遵守。

任何人都可以解釋發生了什麼?

的Python 2.7.5/wxWidgets的2.8.12.1/Mac OS X 10.8.4

回答

0

事實證明,它是Mac OS X的原生對話框實施的限制。

下面使它在OS X上工作。我從來沒有在Windows上試過它,但從其他論壇帖子看來,它會在Windows上按原樣工作。

import wx 
class DisplayText(wx.Dialog): 

    def __init__(self, parent, text="", displayMode=0): 

     # Initialize dialog 
     wx.Dialog.__init__(self, parent, size=(480,320), style=(wx.DIALOG_EX_METAL | wx.STAY_ON_TOP)) 

     # Center form 
     self.Center() 

     # (For Mac) Setup a panel 
     self.panel = wx.Panel(self) 

     # Create text field  
     self.txtField = wx.StaticText(self.panel, label=text, pos=(80,120), size=(320,200), style=wx.ALIGN_CENTRE_HORIZONTAL | wx.ST_NO_AUTORESIZE) 
     self.txtField.SetFont(wx.Font(24, wx.DEFAULT, wx.BOLD, 0))  
     self.txtField.SetAutoLayout(False) 

app = wx.App(False) 

c = DisplayText(None, text="Now is the time for all good men to come to the aid of their country.") 
c.Show() 
app.MainLoop()