wxPython - StaticText Class

Another important element in a GUI interface is a label, a read-only text of one or more lines. It is usually placed on the frame either as an identifier of another widget or as an informative string.
In wxPython, wx.StaticText class object presents a control holding such read-only text. It can be termed as a passive control since it doesn’t produce any event. Wx.StaticText class constructor requires the following usual parameters −
Wx.StaticText(parent, id, label, position, size, style)

Predefined style enumerators are −
The following methods of wx.StaticText class are also useful −
SetLabel()
Sets label of object programmatically
GetLabel()
Returns object’s label
SetForeGroundColour()
Sets color of label’s text
SetBackGroundColour()
Sets label’s background
Wrap()
Wraps label’s text if it can not be accommodated within size.
The above features of StaticText class are demonstrated in the following example. Three StaticText objects are placed in a vertical box sizer.
The first object has multi-line text which is center aligned. The second label’s text is set to wrap around beyond 200 pixels. The third label shows ellipsis (...) in the middle of the text.
In order to set the label’s font, a font object is first created.
Wx.Font(pointsize, fontfamily, fontstyle, fontweight)

Fontfamily parameter takes the following values −
wx.FONTFAMILY_DEFAULT
Chooses a default font
wx.FONTFAMILY_DECORATIVE
Chooses a decorative font
wx.FONTFAMILY_ROMAN
Chooses a formal, serif font
wx.FONTFAMILY_SCRIPT
Chooses a handwriting font
wx.FONTFAMILY_SWISS
Chooses a sans-serif font
wx.FONTFAMILY_MODERN
Chooses a fixed pitch font
wx.FONTFAMILY_TELETYPE
Chooses a teletype (monospaced) font
FontStyle parameter enumerations are −
Wx.FONTSTYLE_NORMAL
The font is drawn without slant
wx.FONTSTYLE_ITALIC
The font is slanted in italic style
wx.FONTSTYLE_SLANT
The font is slanted, but in roman style
FontWeight parameters are −
Wx.FONTWEIGHT_NORMAL
Normal font
wx.FONTWEIGHT_LIGHT
Light font
wx.FONTWEIGHT_BOLD
Bold font
The complete code listing is −
import wx 
 
class Mywin(wx.Frame): 
   def __init__(self, parent, title): 
      super(Mywin, self).__init__(parent, title = title,size = (600,200))
      panel = wx.Panel(self) 
      box = wx.BoxSizer(wx.VERTICAL) 
      lbl = wx.StaticText(panel,-1,style = wx.ALIGN_CENTER) 
		
      txt1 = "Python GUI development" 
      txt2 = "using wxPython" 
      txt3 = " Python port of wxWidget " 
      txt = txt1+"\n"+txt2+"\n"+txt3 
		
      font = wx.Font(18, wx.ROMAN, wx.ITALIC, wx.NORMAL) 
      lbl.SetFont(font) 
      lbl.SetLabel(txt) 
		
      box.Add(lbl,0,wx.ALIGN_CENTER) 
      lblwrap = wx.StaticText(panel,-1,style = wx.ALIGN_RIGHT) 
      txt = txt1+txt2+txt3 
		
      lblwrap.SetLabel(txt) 
      lblwrap.Wrap(200) 
      box.Add(lblwrap,0,wx.ALIGN_LEFT) 
		
      lbl1 = wx.StaticText(panel,-1, style = wx.ALIGN_LEFT | wx.ST_ELLIPSIZE_MIDDLE) 
      lbl1.SetLabel(txt) 
      lbl1.SetForegroundColour((255,0,0)) 
      lbl1.SetBackgroundColour((0,0,0)) 
		
      font = self.GetFont() 
      font.SetPointSize(20) 
      lbl1.SetFont(font) 
		
      box.Add(lbl1,0,wx.ALIGN_LEFT) 
      panel.SetSizer(box) 
      self.Centre() 
      self.Show() 
		
app = wx.App() 
Mywin(None,  'StaticText demo') 
app.MainLoop()
The above code produces the following output −