2011-11-28 108 views
1

我有几个大小测定器和一些量规,我想扩大到该区域或帧的大小,所述面板被放置在工作正常的面板。但是,当StaticText增加到比可用空间更大的尺寸时,我遇到了问题,将面板的边缘和量表从屏幕或可见区域展开。我想知道是否有一种方法可以通过简短地删除文本来停止文本,而不用指定精确的大小,基本上它知道显示区域的边缘在哪里。我可以通过将我更新的文本移动到它们自己的面板来解决这个问题,这样我就可以在其面板上调用Update,使得规格尺寸保持不变,但文本仍然会耗尽我的StaticBox并关闭显示屏,理想。限制静态文本宽度

我可以自动换行下一行,但我注意到,在空间包裹休息和我长的文本是它很可能不会有空格的文件路径,是否有可能在比空间以外的东西打破?

无论是包装或截断将是确定,只是为了阻止它运行在

回答

2

你,你不能完成的静态文本内容,那么我 可以看到的唯一的解决办法是“ellipsize”它,即缩短它通过预先或 将“...”附加到文件路径。要做到这一点,但是,我相信你的 最好的选择是去自行绘制(或简称子类 wx.lib.stattext),测量在筛上部分事件的文字大小为您 控制,如果需要的话,前置/追加“...”到你的路径。

附加了一个概念证明,最后带有“椭圆化”(即附加 “...”,文件名在截尾处截断)。我收集它 将延伸到预先添加“...”而不是 追加。

哦,顺便说一下,在wxPython中2.9你也可以使用 wx.StaticText.Ellipsize来(也许)做同样的事情,虽然我有 从来没有用它自己。

代码示例:

import wx 
from wx.lib.stattext import GenStaticText as StaticText 

if wx.Platform == "__WXMAC__": 
    from Carbon.Appearance import kThemeBrushDialogBackgroundActive 


class EllipticStaticText(StaticText): 

    def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition, size=wx.DefaultSize, 
       style=0, name="ellipticstatictext"): 
     """ 
     Default class constructor. 

     :param `parent`: the L{EllipticStaticText} parent. Must not be ``None``; 
     :param `id`: window identifier. A value of -1 indicates a default value; 
     :param `label`: the text label; 
     :param `pos`: the control position. A value of (-1, -1) indicates a default position, 
     chosen by either the windowing system or wxPython, depending on platform; 
     :param `size`: the control size. A value of (-1, -1) indicates a default size, 
     chosen by either the windowing system or wxPython, depending on platform; 
     :param `style`: the static text style; 
     :param `name`: the window name. 
     """ 

     StaticText.__init__(self, parent, id, label, pos, size, style, name) 

     self.Bind(wx.EVT_SIZE, self.OnSize) 
     self.Bind(wx.EVT_PAINT, self.OnPaint) 
     self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) 


    def OnSize(self, event): 
     """ 
     Handles the ``wx.EVT_SIZE`` event for L{EllipticStaticText}. 

     :param `event`: a `wx.SizeEvent` event to be processed. 
     """ 

     event.Skip() 
     self.Refresh() 


    def OnEraseBackground(self, event): 
     """ 
     Handles the ``wx.EVT_ERASE_BACKGROUND`` event for L{EllipticStaticText}. 

     :param `event`: a `wx.EraseEvent` event to be processed. 

     :note: This is intentionally empty to reduce flicker. 
     """ 

     pass 


    def OnPaint(self, event): 
     """ 
     Handles the ``wx.EVT_PAINT`` event for L{EllipticStaticText}. 

     :param `event`: a `wx.PaintEvent` to be processed. 
     """ 

     dc = wx.BufferedPaintDC(self)   
     width, height = self.GetClientSize() 

     if not width or not height: 
      return 

     clr = self.GetBackgroundColour() 

     if wx.Platform == "__WXMAC__": 
      # if colour is still the default then use the theme's background on Mac 
      themeColour = wx.MacThemeColour(kThemeBrushDialogBackgroundActive) 
      backBrush = wx.Brush(themeColour) 
     else: 
      backBrush = wx.Brush(clr, wx.SOLID) 

     dc.SetBackground(backBrush) 
     dc.Clear() 

     if self.IsEnabled(): 
      dc.SetTextForeground(self.GetForegroundColour()) 
     else: 
      dc.SetTextForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT)) 

     dc.SetFont(self.GetFont()) 

     label = self.GetLabel() 
     text = self.ChopText(dc, label, width) 

     dc.DrawText(text, 0, 0) 


    def ChopText(self, dc, text, max_size): 
     """ 
     Chops the input `text` if its size does not fit in `max_size`, by cutting the 
     text and adding ellipsis at the end. 

     :param `dc`: a `wx.DC` device context; 
     :param `text`: the text to chop; 
     :param `max_size`: the maximum size in which the text should fit. 
     """ 

     # first check if the text fits with no problems 
     x, y = dc.GetTextExtent(text) 

     if x <= max_size: 
      return text 

     textLen = len(text) 
     last_good_length = 0 

     for i in xrange(textLen, -1, -1): 
      s = text[0:i] 
      s += "..." 

      x, y = dc.GetTextExtent(s) 
      last_good_length = i 

      if x < max_size: 
       break 

     ret = text[0:last_good_length] + "..."  
     return ret 


    def Example(): 

     app = wx.PySimpleApp() 
     frame = wx.Frame(None, -1, "EllipticStaticText example ;-)", size=(400, 300)) 

     panel = wx.Panel(frame, -1) 
     sizer = wx.BoxSizer(wx.VERTICAL) 

     elliptic = EllipticStaticText(panel, -1, r"F:\myreservoir\re\util\python\hm_evaluation\data\HM_Evaluation_0.9.9.7.exe") 
     whitePanel = wx.Panel(panel, -1) 
     whitePanel.SetBackgroundColour(wx.WHITE) 

     sizer.Add(elliptic, 0, wx.ALL|wx.EXPAND, 10) 
     sizer.Add(whitePanel, 1, wx.ALL|wx.EXPAND, 10) 

     panel.SetSizer(sizer) 
     sizer.Layout() 

     frame.CenterOnScreen() 
     frame.Show() 

     app.MainLoop() 


    if __name__ == "__main__": 

     Example() 
+0

谢谢!那很完美!!而且我甚至不需要进行修改来预先设置椭圆,因为它已经做了:)在用椭圆检查字符串长度的地方,它总是非常适合! – GP89

+0

只是一个简单的问题,因为我的文字是在StaticBox(这对MAC淡灰色透明背景)的颜色OnPaint方法给灭了,我试着取出它真正的背景颜色,但随后其位油漆白色,我尝试清除,但文本不清晰,它在顶部涂料。希望我能在一分钟内弄清楚,但似乎无法得到它? – GP89

+0

我想你会想wx.PaintDC更换wx.BufferedPaintDC在OnPaint方法,去除OnEraseBackground绑定和方法,而这个'self.SetBackgroundStyle(wx.BG_STYLE_SYSTEM)'添加到您的'__init__'方法。 – Infinity77