How can I get the user to enter text

Moving beyond the mere display of static text, we'll begin discussing user interaction when entering text. The wxPython class for the text entry widget is wx.TextCtrl, which allows both single-line and multi-line text entry. It can also act as a password control, masking the keys pressed. If supported by the platform, the wx.TextCtrl also provides rich text display, with multiple text styles defined and displayed. Figure 7.2 displays a sample of wx.TextCtrl as a single-line control, both with and without password masking.

Figure 7.2 Examples of the single line text control, both plain and password

Figure 7.2 Examples of the single line text control, both plain and password

In the next section, we'll illustrate how to create the text, then discuss the style options for text controls.

How to do it

Listing 7.2 displays the code used to generate figure 7.2.

Listing 7.2 The wx.TextCtrl single line example import wx class TextFrame(wx.Frame):

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

basicLabel = wx.StaticText(panel, -1, "Basic Control:") basicText = wx.TextCtrl(panel, -1, "I've entered some text!", size=(175, -1)) basicText.SetInsertionPoint(0)

pwdLabel = wx.StaticText(panel, -1, "Password:")

pwdText = wx.TextCtrl(panel, -1, "password", size=(175, -1), style=wx.TE_PASSWORD) sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6) sizer.AddMany([basicLabel, basicText, pwdLabel, pwdText]) panel.SetSizer(sizer)

if __name__ == '__main__': app = wx.PySimpleApp() frame = TextFrame() frame.Show() app.MainLoop()

The wx.TextCtrl class has a slightly more elaborate constructor than the wx.Window parent class, adding two arguments:

wx.TextCtrl(parent, id, value = "", pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, validator=wx.DefaultValidator name=wx.TextCtrlNameStr)

The parent, id, pos, size, style, and name arguments are all identical to those in the wx.Window constructor. The value argument is the initial value of the text displayed in the control.

The validator argument is used for a wx.Validator. A validator is often used to filter data to ensure that only acceptable data is entered into the control. Validators are discussed in more detail in chapter 9.

Using single line text control styles

In this section, we'll begin discussing some of the unique text control style bits. Table 7.3 describes the style flags that are used for a single-line text control.

Table 7.3 The style bits for a single line wx.TextCtrl




The text is centered within the control.


The text is left justified within the control. This is the default behavior.


The name of this option parses to "no hide sel," in case you were having trouble decoding it. It's a Windows option to override a default behavior of the Windows text widget, namely that it doesn't highlight the selected text unless the widget has focus. With this option selected, the widget will always highlight the text. Has no effect on other systems.


The text entered will not be displayed, but instead masked by asterisks.


If this bit is specified, a text enter event is triggered when the user presses the enter key within the control. Otherwise, the keypress is managed internally by either the text control or the dialog.


If this bit is specified, a normal character event will be created for a tab key pressed (generally meaning a tab will be inserted into the text). If not specified, then the tab will be managed by the dialog, usually for keyboard navigation between controls.


The text control is read-only, and cannot be modified by user input.


The text is right-justified within the control.

Like other style flags, these can be combined using the | operator, although the three alignment flags are mutually exclusive.

The text control automatically manages the user's keypress and mouse events to add text and to move the insertion point. The following common control combinations are included:

Was this article helpful?

0 -1

Post a comment