限制StaticText宽度
我有一个面板,里面有几个调整大小的控件和一些仪表,我希望它们能够扩展到面板所在区域的大小,这个功能现在是正常的。不过,我遇到了一个问题,就是当一个静态文本(StaticText)变得太大,超出了可用空间时,会把面板和仪表的边缘推到屏幕外或者可视区域外。我在想有没有办法让文本在达到边缘时自动截断,显示成“...”的样子,而不需要指定一个具体的大小,基本上就是让它知道显示区域的边界在哪里。我可以通过把需要更新的文本移动到自己的面板上来解决这个问题,这样我可以对这些面板调用更新,但文本还是会超出我的静态框(StaticBox)并且跑到显示区域外,这样并不好。
我可以把文本换行,但我注意到换行是根据空格来进行的,而我的长文本是一个文件路径,通常是没有空格的,能不能用其他的方式来换行呢?
无论是换行还是截断都可以,只要能防止文本超出边界就行。
1 个回答
2
如果你不能让StaticText的内容换行,那么我能想到的唯一解决办法就是“省略”它,也就是在文件路径的前面或后面加上“...”,让它变短。不过,要做到这一点,我觉得最好的办法是自己绘制(或者简单地说,继承wx.lib.stattext),在你的控件的OnSize事件中测量文本的大小,如果需要的话,就在路径的前面或后面加上“...”。
这里附上一个概念验证的例子,最后的部分是“省略化”(也就是在后面加上“...”并且文件名在最后被截断)。我觉得把“...”加到前面应该也很简单。
哦,对了,在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()