- wxPython Tutorial
- wxPython - Home
- wxPython - Introduction
- wxPython - Environment
- wxPython - Hello World
- wxPython - GUI Builder Tools
- wxPython - Major Classes
- wxPython - Event Handling
- wxPython - Layout Management
- wxPython - Buttons
- wxPython - Dockable Windows
- Multiple Document Interface
- wxPython - Drawing API
- wxPython - Drag and Drop
- wxPython Resources
- wxPython - Quick Guide
- wxPython - Useful Resources
- wxPython - Discussion
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 −
wx.ALIGN_LEFT | Controls the alignment of label within the size |
wx.ALIGN_RIGHT | |
wx.ALIGN_CENTER | |
wx.ST_NO_AUTORESIZE | Prevents auto resizing of the label |
wx.ST_ELLIPSIZE_START | Ellipsis (…) appears at the start, in the middle or at the end, if the size of the text is bigger than the label size |
wx.ST_ELLIPSIZE_MIDDLE | |
wx.ST_ELLIPSIZE_END |
The following methods of wx.StaticText class are also useful −
S.N. | Methods & Description |
---|---|
1 | SetLabel() Sets label of object programmatically |
2 | GetLabel() Returns object’s label |
3 | SetForeGroundColour() Sets color of label’s text |
4 | SetBackGroundColour() Sets label’s background |
5 | 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 −
S.N. | Parameters & Description |
---|---|
1 | wx.FONTFAMILY_DEFAULT Chooses a default font |
2 | wx.FONTFAMILY_DECORATIVE Chooses a decorative font |
3 | wx.FONTFAMILY_ROMAN Chooses a formal, serif font |
4 | wx.FONTFAMILY_SCRIPT Chooses a handwriting font |
5 | wx.FONTFAMILY_SWISS Chooses a sans-serif font |
6 | wx.FONTFAMILY_MODERN Chooses a fixed pitch font |
7 | wx.FONTFAMILY_TELETYPE Chooses a teletype (monospaced) font |
FontStyle parameter enumerations are −
S.N. | Parameters & Description |
---|---|
1 | Wx.FONTSTYLE_NORMAL The font is drawn without slant |
2 | wx.FONTSTYLE_ITALIC The font is slanted in italic style |
3 | wx.FONTSTYLE_SLANT The font is slanted, but in roman style |
FontWeight parameters are −
S.N. | Parameters & Description |
---|---|
1 | Wx.FONTWEIGHT_NORMAL Normal font |
2 | wx.FONTWEIGHT_LIGHT Light font |
3 | 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 −