How do I display static text

Perhaps the most basic task for any UI toolkit is drawing plain text on the screen. In wxPython, this is accomplished with the wx.StaticText class. Figure 7.1 displays the static text control.

In a wx.StaticText, you can change the alignment, font, and color of the text. A single static text widget can contain multiple lines of text, however, it cannot handle multiple fonts or styles. For multiple fonts or styles, use a more elaborate text control, such as wx.html.HTMLWindow, described in chapter 16. To display multiple lines within a static text control, include a string with newline characters inside it, and make the control big enough to display all the text. One feature that you cannot see from just the figure is that the wx.StaticText window never receives or responds to mouse events, and never takes the user focus.

Wxpython Example
Figure 7.1 Samples of wx.StaticText, including font, alignment, and color changes

How to display static text

Listing 7.1 displays the code that produced figure 7.1.

Listing 7.1 A basic example of how to use static text import wx

1, 'Static Text Example',

Viewing basic static text is an example of static text", <-

Designating reversed colors

Designating center aligned class StaticTextFrame(wx.Frame):

wx.Frame._init_(self, None, size=(4 00, 300)) panel = wx.Panel(self, -1) wx.StaticText(panel, -1, "This (100, 10)) rev = wx.StaticText(panel, -1,

"Static Text With Reversed Colors" (100, 30)) rev.SetForegroundColour('white') rev.SetBackgroundColour('black') center = wx.StaticText(panel, -1, "align center", (100, 50), (160, -1), wx.ALIGN_CENTER) center.SetForegroundColour('white') center.SetBackgroundColour('black') right = wx.StaticText(panel, -1, "align right", (100, 70), (160, -1), wx.ALIGN_RIGHT) right.SetForegroundColour('white') right.SetBackgroundColour('black') str = "You can also change the font, text = wx.StaticText(panel, -1, str, font = wx.Font(18, wx.DECORATIVE, wx.ITALIC, wx.NORMAL) text.SetFont(font) wx.StaticText(panel, -1,

"Your text\ncan be split\n" "over multiple lines\n\neven blank ones", wx.StaticText(panel, -1,

"Multi-line text\ncan also\n" "be right aligned\n\neven with a blank", (220,150) style=wx.ALIGN_RIGHT)

Designating right aligned

Defining a new font

Displaying multi-lines

^^ Displaying aligned multi-lines if __name__ == '__main__': app = wx.PySimpleApp() frame = StaticTextFrame() frame.Show() app.MainLoop()

The constructor for wx.StaticText is identical to the basic wxWidget constructors, as in the following:

wx.StaticText(parent, id, label, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name="staticText")

Table 7.1 displays what the parameters areā€”most wxPython widgets have a similar set in their constructor. Refer to the widget discussion in chapter 2 for a more detailed description of constructor parameters.

Table 7.1 Parameters of the wx.StaticText constructor

Parameter

Purpose

parent

The containing widget

id

The wxPython identifier. To automatically create a unique identifier, use -1

label

Contains the text that you want to display in the static control.

pos

The position of the widget as a wx.Point object or a Python tuple

size

The size of the widget as a wx.Size object or a Python tuple

style

The style flag

name

Name used for finding the object

In the next section, we'll discuss style flags in more detail. Working with the styles

All of the methods called on the static text instances in listing 7.1 belong to the parent wx.Window class; wx.StaticText defines no new methods of its own. A few style bits are specific to wx.StaticText, and they are listed in table 7.2.

Table 7.2 Style bit flags unique to the wx.StaticText class

Style

Description

wx.ALIGN_CENTER

Centers the static text within the size rectangle of the static text widget.

wx.ALIGN_LEFT

The text is left-aligned in the widget. This is the default.

wx.ALIGN_RIGHT

The text is right-aligned in the widget.

wx.ST_NO_AUTORESIZE

If this bit is used, the static text widget will not resize itself after the text is changed with SetLabel(). You would use this in conjunction with a center or right-aligned control to preserve the alignment.

The wx.StaticText control overrides SetLabel() in order to resize itself based on the new text, which happens unless the wx.st_no_autoresize style is set.

When creating a single line static text control with a center or right alignment, you should explicitly set the size of the control in the constructor. Specifying the size prevents wxPython from automatically sizing the control. The wxPython default size is the minimum rectangle surrounding the text. Since by default the text control is no larger than the text contained, and there is no blank space to show the alignment, it is irrelevant whether the control is left, right, or center aligned. To change the text in the widget dynamically during the program without changing the size of the control, set the wx.st_no_autoresize style. This prevents the widget from resizing itself back to a minimum rectangle after the text is reset. If the static text is inside a dynamic layout, changing its size may move other widgets on the screen, creating a distraction for the user.

Other techniques for text display

There are other ways of adding static text onto your display. One is the wx.lib.stattext.GenStaticText class, which is a Python-only reimplementation of wx.StaticText. It is more consistent cross-platform than the standard C + + version, and it receives mouse events. It's also preferable when you want to subclass and create your own static text control.

You can draw text directly to your device context using the DrawText(text, x, y) method or the DrawRotatedText(text, x, y, angle) method. The latter is the easiest way to add angled text to your display, although a subclass of GenStatic-Text that handles rotation is also available. Device contexts were covered briefly in chapter 6, and will be covered in more detail in chapter 12.

Was this article helpful?

0 -4

Responses

  • ella
    Why does trying to setforegroundcolor on a statictext give me an attributeerror?
    8 years ago
  • Mirabella
    How to use wx.st_no_autoresize?
    8 years ago
  • Fulvus
    How to give colour to the static text in wxpython?
    7 years ago
  • tyyne
    How to center static text wxpython?
    5 years ago
  • dominique goulette
    How to display a variable to a wxpython widget?
    5 years ago
  • Izaak
    How to add a variable to static text in wxpython?
    5 years ago
  • TONYA
    How to add static text in pygame?
    4 years ago
  • callan
    How to create single line static text control panel?
    3 years ago
  • tanja
    How to change font in wxpython?
    3 years ago
  • doda
    How to show text in wxpython?
    11 months ago
  • tiana
    How to dynamic change wx.html.htmlwindow?
    9 months ago
  • wilibald
    How to locate objects on the screen using wxpython?
    12 days ago

Post a comment