How do I create a multiline or styled text control

You can create a multi-line text control using the wx.te_multiline style flag. If the native widget has support for styles, you can change font and color styles within the text managed by the control, which is sometimes called rich text. For other platforms, the calls to set styles are simply ignored. Figure 7.3 displays an example of multi-line text controls.

Listing 7.3 contains the code used to create figure 7.3. Typically, creating a multi-line control is handled by setting the wx.te_multiline style flag. Later in this section, we'll discuss using rich text styles.

"Text Entry Example I - JI.O.K>


Here is a looooooooooooooong line of text set in the control.

See that it wrapped, and that this line s after a blank]

Rich Text

If supporte^y the native control, this is HBHSl. and this is a different font.

Figure 7.3 Examples of multiline text controls, both with and without rich text

Listing 7.3 Creating a multi-line text control import wx class TextFrame(wx.Frame):

wx.Frame._init_(self, None, -1, 'Text Entry Example', size=(3 00, 250)) panel = wx.Panel(self, -1)

multiLabel = wx.StaticText(panel, -1, "Multi-line")

multiText = wx.TextCtrl(panel, -1, <1— Creating a text control

"Here is a looooooooooooooong line "

"of text set in the control.\n\n"

"See that it wrapped, and that "

"this line is after a blank", size=(2 00, 100), style=wx.TE_MULTILINE)

multiText.SetInsertionPoint(0) <1—, „ ... .. . .

| Setting the cursor point richLabel = wx.StaticText(panel, -1, "Rich Text") richText = wx.TextCtrl(panel, -1, <— Creating a rich text control "If supported by the native control, " "this is reversed, and this is a different font.", size=(2 00, 100), style=wx.TE_MULTILINE|wx.TE_RICH2) richText.SetInsertionPoint(0)

richText.SetStyle(44, 52, wx.TextAttr("white" points = richText.GetFont().GetPointSize() f = wx.Font(points + 3, wx.ROMAN, wx.ITALIC, wx.BOLD, True) richText.SetStyle(68, 82, wx.TextAttr("blue", I Setting a style in wx.NullColour, f)) <-

sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)

Setting text styles

_I Creating a font the new font sizer.AddMany([multiLabel, multiText, richLabel, richText]) panel.SetSizer(sizer)

app = wx.PySimpleApp() frame = TextFrame() frame.Show() app.MainLoop()

Using multiple or rich text styles

In addition to wx.te_multiline, there are other style flags that are only meaningful in the context of a multi-line or rich text control. Table 7.5 lists those window styles.

Table 7.5 The style bits for wx.TextCtrl, when used as a multiple line control




If the text control is multi-line, and if this style is declared, long lines will be horizontally scrolled instead of wrapped. This option is ignored in GTK+.


If the rich text option is set and the platform supports it, this style causes an event to be generated when the user mouses over or clicks on a URL in the text.


Another name for wx.hscroll.


A contrast to wx.te_wordwrap. Lines which are wrapped can be wrapped at any character. Some operating systems may ignore this style.


The text control will display multiple lines.


Under Windows, use the rich text control as the underlying widget. This allows the use of styled text.


Under Windows, use the most recent version of the rich text control as the underlying widget.


Contrast to wx.te_linewrap, lines which wrap will only do so at word boundaries. This option is ignored on many systems.

Remember that style bits can be combined, so the multi-line rich text control in this example is declared with a style of wx.te_multiline | wx.te_rich2.

The text styles used in a wx.TextCtrl widget are instances of the class wx.Text-Attr. A wx.TextAttr instance has a text color, a background color, and a font, all of which can be specified in the constructor as in the following:

wx.TextAttr(colText, colBack=wx.NullColor, font=wx.NullFont)

The text and background colors are wxPython wx.Color objects that can be specified with a string naming the color or a tuple with the (red, green, blue) values of the color. The wx.NullColor indicates that the existing background color of the control should be used. The font is a wx.Font object, which we'll discuss in the next subsection. The wx.NullFont object indicates that the current default font should be used.

The wx.TextAttr class has getter methods for the attributes GetBackground-Colour(), GetFont(), and GetTextColour(), as well as Boolean existence methods for HasBackgroundColour(), HasFont(), and HasTextColour(). If the attribute contains a default value, the existence methods return False. The lsDefault() method returns true if all three attributes contain default values. The class does not have setter methods, since instances of wx.TextAttr are immutable. To change the style of text, you must create an instance.

To use a text style, call SetDefaultStyle(style) or SetStyle(start, end, style). The first method sets the current style of the control. Any text inserted into the control, either by typing or by using AppendText() or WriteText(), is displayed in that style. If any of the attributes of the style are default, the current value for that style is kept. However, if all of the attributes of the style are default, the default style is reinstated. The SetStyle() method is similar, but takes effect immediately on the text between the start and end positions. Default attributes in the style argument are resolved by verifying the current default style for the control. Listing 7.3 uses the following line of code to reverse the colors on several characters of text:

richText.SetStyle(44, 52, wx.TextAttr("white", "black"))

The background color becomes white, and the text color for those characters becomes black.

Table 7.6 lists the methods of wx.TextCtrl, which are useful in manipulating multi-line controls and rich text.

Table 7.6 Multi-line and style methods of wx.TextCtrl



GetDefaultStyle() SetDefaultStyle(style)

See the earlier part of this section for a description of default styles.


Returns the integer length of the given line.


Returns the text of the given line

Table 7.6 Multi-line and style methods of wx.Textctrl (continued)




Returns the number of lines in the control. For a single-line control, returns 1.

IsMultiLine() IsSingleLineO

Boolean methods for determining state of the control


Given an integer position within the text, returns a tuple with the (col, row) index of the position. The column and row indexes both start at 0.

SetStyle(start, end, style)

Immediately changes the style for the given range of text.


Causes a multi-line control to scroll such that the given position is in view.

XYToPosition(x, y)

Inverse of PositionToXY—given a row and column, it returns the integer position.

Creating styles is much more flexible if you can use arbitrary fonts in the system. Next, we'll show you how to create and use font instances.

Was this article helpful?

+1 -1


  • robert
    How to display line and column number python text control?
    7 years ago
  • biniam
    Can you color text control boxes in wxpython?
    7 years ago
  • Bowman
    How to append items in a multiline text control to list wxpython?
    7 years ago
  • Blake
    How to make a textcontrol in wxpython?
    6 years ago
  • lalli
    How to set multiple styles wxpython?
    3 years ago

Post a comment