wx.TextCtrl

Inheritance diagram for wx.TextCtrl:



Description

A text control allows text to be displayed and edited. It may be single line or multi-line.

Known Subclasses

wx.SearchCtrl

Window Styles

Window Style Description
wx.TE_PROCESS_ENTER The control will generate the event wx.wxEVT_COMMAND_TEXT_ENTER (otherwise pressing Enter key is either processed internally by the control or used for navigation between dialog controls).
wx.TE_PROCESS_TAB The control will receive wx.EVT_CHAR events for TAB pressed – normally, TAB is used for passing to the next control in a dialog instead. For the control created with this style, you can still use Ctrl-Enter to pass to the next control from the keyboard.
wx.TE_MULTILINE The text control allows multiple lines.
wx.TE_PASSWORD The text will be echoed as asterisks.
wx.TE_READONLY The text will not be user-editable.
wx.TE_RICH Use rich text control under Win32, this allows to have more than 64KB of text in the control even under Win9x. This style is ignored under other platforms.
wx.TE_RICH2 Use rich text control version 2.0 or 3.0 under Win32, this style is ignored under other platforms
wx.TE_AUTO_URL Highlight the URLs and generate the wx.TextUrlEvents when mouse events occur over them. This style is only supported for wx.TE_RICH Win32 and multi-line wxGTK2 text controls.
wx.TE_NOHIDESEL By default, the Windows text control doesn’t show the selection when it doesn’t have focus - use this style to force it to always show it. It doesn’t do anything under other platforms.
wx.HSCROLL A horizontal scrollbar will be created and used, so that text won’t be wrapped. No effect under wxGTK1.
wx.TE_LEFT The text in the control will be left-justified (default).
wx.TE_CENTRE The text in the control will be centered (currently wxMSW and wxGTK2 only).
wx.TE_RIGHT The text in the control will be right-justified (currently wxMSW and wxGTK2 only).
wx.TE_DONTWRAP Same as wx.HSCROLL style: don’t wrap at all, show horizontal scrollbar instead.
wx.TE_CHARWRAP Wrap the lines too long to be shown entirely at any position (wxUniv and wxGTK2 only).
wx.TE_WORDWRAP Wrap the lines too long to be shown entirely at word boundaries (wxUniv and wxGTK2 only).
wx.TE_BESTWRAP Wrap the lines at word boundaries or at any other character if there are words longer than the window width (this is the default).
wx.TE_CAPITALIZE On PocketPC and Smartphone, causes the first letter to be capitalized.

Note

Note that alignment styles (wx.TE_LEFT, wx.TE_CENTRE and wx.TE_RIGHT) can be changed dynamically after control creation on wxMSW and wxGTK. wx.TE_READONLY, wx.TE_PASSWORD and wrapping styles can be dynamically changed under wxGTK but not wxMSW. The other styles can be only set during control creation.

Multi-line text controls support the styles, i.e. provide a possibility to set colours and font for individual characters in it (note that under Windows wx.TE_RICH style is required for style support). To use the styles you can either call SetDefaultStyle before inserting the text or call SetStyle later to change the style of the text already in the control (the first solution is much more efficient).

In either case, if the style doesn’t specify some of the attributes (for example you only want to set the text colour but without changing the font nor the text background), the values of the default style will be used for them. If there is no default style, the attributes of the text control itself are used.

So the following code correctly describes what it does: the second call to SetDefaultStyle doesn’t change the text foreground colour (which stays red) while the last one doesn’t change the background colour (which stays grey):

text.SetDefaultStyle(wx.TextAttr(wx.RED))
text.AppendText("Red text\n")
text.SetDefaultStyle(wx.TextAttr(wx.NullColour, wx.LIGHT_GREY))
text.AppendText("Red on grey text\n")
text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
text.AppendText("Blue on grey text\n")

Text Format

The multiline text controls always store the text as a sequence of lines separated by \n characters, i.e. in the Unix text format even on non-Unix platforms. This allows the user code to ignore the differences between the platforms but at a price: the indices in the control such as those returned by GetInsertionPoint or GetSelection can not be used as indices into the string returned by GetValue as they’re going to be slightly off for platforms using \r\n as separator (as Windows does), for example.

Instead, if you need to obtain a substring between the 2 indices obtained from the control with the help of the functions mentioned above, you should use GetRange. And the indices themselves can only be passed to other methods, for example SetInsertionPoint or SetSelection.

To summarize: never use the indices returned by (multiline) wx.TextCtrl as indices into the string it contains, but only as arguments to be passed back to the other wx.TextCtrl methods.

Event Handling

The following commands are processed by default event handlers in wx.TextCtrl: wx.ID_CUT, wx.ID_COPY, wx.ID_PASTE, wx.ID_UNDO, wx.ID_REDO. The associated UI update events are also processed automatically, when the control has the focus.

To process input from a text control, use these event handler macros to direct input to member functions that take a wx.CommandEvent argument.

Event Name Description
wx.EVT_TEXT(id, func) Respond to a wx.wxEVT_COMMAND_TEXT_UPDATED event, generated when the text changes. Notice that this event will be sent when the text controls contents changes - whether this is due to user input or comes from the program itself (for example, if SetValue() is called); see ChangeValue() for a function which does not send this event.
wx.EVT_TEXT_ENTER(id, func) Respond to a wx.wxEVT_COMMAND_TEXT_ENTER event, generated when enter is pressed in a text control (which must have wx.TE_PROCESS_ENTER style for this event to be generated).
wx.EVT_TEXT_URL(id, func) A mouse event occurred over an URL in the text control (wxMSW and wxGTK2 only)
wx.EVT_TEXT_MAXLEN(id, func) User tried to enter more text into the control than the limit set by SetMaxLength.

Control Appearance


wxMSW

wxMSW

wxMAC

wxMAC

wxGTK

wxGTK


Class API

Methods

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

Constructor, creating and showing a text control.

Parameters:


Returns:

wx.TextCtrl


AppendText(text)

Appends the text to the end of the text control.

Parameters:

  • text (string): Text to write to the text control.

Note

After the text is appended, the insertion point will be at the end of the text control. If this behaviour is not desired, the programmer should use GetInsertionPoint and SetInsertionPoint.

See also

WriteText


CanCopy()

Returns True if the selection can be copied to the clipboard.


Returns:

bool


CanCut()

Returns True if the selection can be cut to the clipboard.


Returns:

bool


CanPaste()

Returns True if the contents of the clipboard can be pasted into the text control. On some platforms (Motif, GTK) this is an approximation and returns True if the control is editable, False otherwise.


Returns:

bool


CanRedo()

Returns True if there is a redo facility available and the last operation can be redone.


Returns:

bool


CanUndo()

Returns True if there is an undo facility available and the last operation can be undone.


Returns:

bool


ChangeValue(value)

Sets the text value and marks the control as not-modified (which means that IsModified would return False immediately after the call to ChangeValue).

This function is new since wxWidgets version 2.7.1

Parameters:

  • value (string): The new value to set. It may contain newline characters if the text control is multi-line.

Note

Note that this function will not generate the wx.wxEVT_COMMAND_TEXT_UPDATED event. This is the only difference with SetValue.


Clear()
Clears the text in the control.

Note

Note that this function will generate a wx.wxEVT_COMMAND_TEXT_UPDATED event.


Copy()
Copies the selected text to the clipboard under Motif and MS Windows.

Cut()
Copies the selected text to the clipboard and removes the selection.

DiscardEdits()
Resets the internal ‘modified’ flag as if the current edits had been saved.

EmulateKeyPress(event)

This functions inserts into the control the character which would have been inserted if the given key event had occurred in the text control.

The event object should be the same as the one passed to EVT_KEY_DOWN handler previously by wxWidgets.

Parameters:


Returns:

bool

Note

Please note that this function doesn’t currently work correctly for all keys under any platform but MSW.


GetDefaultStyle()

Returns the style currently used for the new text.


Returns:

wx.TextAttr

See also

SetDefaultStyle


GetInsertionPoint()

Returns the insertion point. This is defined as the zero based index of the character position to the right of the insertion point.

For example, if the insertion point is at the end of the text control, it is equal to both len(GetValue()) and GetLastPosition. The following code snippet safely returns the character at the insertion point or the zero character if the point is at the end of the control:

def GetCurrentChar(myTextCtrl):

    if myTextCtrl.GetInsertionPoint() == myTextCtrl.GetLastPosition():
        return '\0'

    return myTextCtrl.GetValue()[myTextCtrl.GetInsertionPoint()]

Returns:

long


GetLastPosition()

Returns the zero based index of the last position in the text control, which is equal to the number of characters in the control.


Returns:

int


GetLineLength(lineNo)

Gets the length of the specified line, not including any trailing newline character(s).

Parameters:

  • lineNo (long): Line number (starting from zero).

Returns:

int


GetLineText(lineNo)

Returns the contents of a given line in the text control, not including any trailing newline character(s).

Parameters:

  • lineNo (long): The line number, starting from zero.

Returns:

string


GetNumberOfLines()

Returns the number of lines in the text control buffer.


Returns:

int

Note

Note that even empty text controls have one line (where the insertion point is), so GetNumberOfLines() never returns 0.


GetRange(from, to)

Returns the string containing the text starting in the positions from and up to to in the control. The positions must have been returned by another wx.TextCtrl method.

Parameters:

  • from (long)
  • to (long)

Returns:

string

Note

Please note that the positions in a multiline wx.TextCtrl do not correspond to the indices in the string returned by GetValue because of the different new line representations (CR or CR LF) and so this method should be used to obtain the correct results instead of extracting parts of the entire value. It may also be more efficient, especially if the control contains a lot of data.


GetSelection()

Gets the current selection span. If the returned values are equal, there was no selection.


Returns:

(from, to)

Note

Please note that the indices returned may be used with the other wx.TextCtrl methods but don’t necessarily represent the correct indices into the string returned by GetValue for multiline controls under Windows (at least) you should use GetStringSelection to get the selected text.


GetString(from, to)

Parameters:

  • from (long)
  • to (long)

Returns:

string


GetStringSelection()

Gets the text currently selected in the control. If there is no selection, the returned string is empty.


Returns:

string


GetStyle(position, style)

Returns the style at this position in the text control. Not all platforms support this function.

Parameters:


Returns:

bool

See also

SetStyle, wx.TextAttr


GetValue()

Gets the contents of the control. Notice that for a multiline text control, the lines will be separated by (Unix-style) \n characters, even under Windows where they are separated by a \r\n sequence in the native control.


Returns:

string


HideNativeCaret()
No docstrings available for this method.

HitTest(pt)

Find the character position in the text coresponding to the point given in pixels.

Possible return values for result are:

  • wx.TE_HT_UNKNOWN —> this means HitTest() is simply not implemented
  • wx.TE_HT_BEFORE —> either to the left or upper
  • wx.TE_HT_ON_TEXT —> directly on
  • wx.TE_HT_BELOW —> below [the last line]
  • wx.TE_HT_BEYOND —> after [the end of line]

Parameters:


Returns:

(result, col, row)

Note

pt is in device coords but is not adjusted for the client area origin nor scrolling.


HitTestPos(pt)

Find the character position in the text coresponding to the point given in pixels.

Possible return values for result are:

  • wx.TE_HT_UNKNOWN —> this means HitTest() is simply not implemented
  • wx.TE_HT_BEFORE —> either to the left or upper
  • wx.TE_HT_ON_TEXT —> directly on
  • wx.TE_HT_BELOW —> below [the last line]
  • wx.TE_HT_BEYOND —> after [the end of line]

Parameters:


Returns:

(result, position)

Note

pt is in device coords but is not adjusted for the client area origin nor scrolling.


IsEditable()

Returns True if the controls contents may be edited by user (note that it always can be changed by the program), i.e. if the control hasn’t been put in read-only mode by a previous call to SetEditable.


Returns:

bool


IsEmpty()

Returns True if the control is currently empty. This is the same as len(GetValue()) == 0 but can be much more efficient for the multiline controls containing big amounts of text.

This function is new since wxWidgets version 2.7.1


Returns:

bool


IsModified()

Returns True if the text has been modified by user.


Returns:

bool

Note

Note that calling SetValue doesn’t make the control modified.

See also

MarkDirty


IsMultiLine()

Returns True if this is a multi line edit control and False otherwise.


Returns:

bool

See also

IsSingleLine


IsSingleLine()

Returns True if this is a single line edit control and False otherwise.


Returns:

bool

See also

IsMultiLine


LoadFile(fileName, fileType=wx.TEXT_TYPE_ANY)

Loads and displays the named file, if it exists.

Returns True if successful, False otherwise.

Parameters:

  • fileName (string): The filename of the file to load.
  • fileType (int): The type of file to load. This is currently ignored in wx.TextCtrl.

Returns:

bool


MacCheckSpelling(check)

Parameters:

  • check (bool)

MarkDirty()
Mark text as modified (dirty).

See also

IsModified


Paste()
Pastes text from the clipboard to the text item.

PositionToXY(pos)

Converts given position to a zero-based column, line number pair.

Parameters:

  • pos (long): Position.

Returns:

(x, y)

See also

XYToPosition


Redo()
If there is a redo facility and the last operation can be redone, redoes the last operation. Does nothing if there is no redo facility.

Remove(from, to)

Removes the text starting at the first given position up to (but not including) the character at the last position.

Parameters:

  • from (long): The first position.
  • to (long): The last position.

Replace(from, to, value)

Replaces the text starting at the first position up to (but not including) the character at the last position with the given text.

Parameters:

  • from (long): The first position.
  • to (long): The last position.
  • value (string): The value to replace the existing text with.

SaveFile(fileName, fileType=wx.TEXT_TYPE_ANY)

Saves the contents of the control in a text file.

Returns True if successful, False otherwise.

Parameters:

  • fileName (string): The filename of the file to save.
  • fileType (int): The type of file to save. This is currently ignored in wx.TextCtrl.

Returns:

bool


SelectAll()
No docstrings available for this method.

SendTextUpdatedEvent()
No docstrings available for this method.

SetDefaultStyle(style)

Changes the default style to use for the new text which is going to be added to the control using WriteText or AppendText.

If either of the font, foreground, or background colour is not set in style, the values of the previous default style are used for them. If the previous default style didn’t set them neither, the global font or colours of the text control itself are used as fall back.

However if the style parameter is the default wx.TextAttr, then the default style is just reset (instead of being combined with the new style which wouldn’t change it at all).

Returns True if successful, False if an error occurred - may also mean that the styles are not supported under this platform.

Parameters:


Returns:

bool

See also

GetDefaultStyle


SetEditable(editable)

Makes the text item editable or read-only, overriding the wx.TE_READONLY flag.

Parameters:

  • editable (bool): If True, the control is editable. If False, the control is read-only.

See also

IsEditable


SetInsertionPoint(pos)

Sets the insertion point at the given position.

Parameters:

  • pos (int): position to set.

SetInsertionPointEnd()
Sets the insertion point at the end of the text control. This is equivalent to SetInsertionPoint ( GetLastPosition () ).

SetMaxLength(len)

This function sets the maximum number of characters the user can enter into the control. In other words, it allows to limit the text value length to len not counting the terminating NUL character.

If len is 0, the previously set max length limit, if any, is discarded and the user may enter as much text as the underlying native text control widget supports (typically at least 32Kb).

If the user tries to enter more characters into the text control when it already is filled up to the maximal length, a wx.wxEVT_COMMAND_TEXT_MAXLEN event is sent to notify the program about it (giving it the possibility to show an explanatory message, for example) and the extra input is discarded.

Note

Note that under GTK+, this function may only be used with single line text controls.


SetModified(modified)

Marks the control as being modified by the user or not.

Parameters:

  • modified (bool)

SetSelection(from, to)

Selects the text starting at the first position up to (but not including) the character at the last position.

If both parameters are equal to -1 all text in the control is selected.

Parameters:

  • from (long): The first position.
  • to (long): The last position.

SetStyle(start, end, style)

Changes the style of the given range. If any attribute within style is not set, the corresponding attribute from GetDefaultStyle is used.

Parameters:

  • start (long): The start of the range to change.
  • end (long): The end of the range to change.
  • style (wx.TextAttr): The new style for the range.

Returns:

bool

See also

GetStyle, wx.TextAttr


SetValue(value)

Sets the text value and marks the control as not-modified (which means that IsModified would return False immediately after the call to SetValue).

This function is deprecated and should not be used in new code. Please use the ChangeValue function instead.

Parameters:

  • value (string): The new value to set. It may contain newline characters if the text control is multi-line.

Note

Note that this function will generate a wx.wxEVT_COMMAND_TEXT_UPDATED event.

See also

ChangeValue


ShowNativeCaret(show=True)

Parameters:

  • show (bool)

Returns:

bool


ShowPosition(pos)

Makes the line containing the given position visible.

Parameters:

  • pos (long): The position that should be visible.

Undo()
If there is an undo facility and the last operation can be undone, undoes the last operation. Does nothing if there is no undo facility.

WriteText(text)

Writes the text into the text control at the current insertion position.

Parameters:

  • text (string): Text to write to the text control.

Note

Newlines in the text string are the only control stringacters allowed, and they will cause appropriate line breaks. See AppendText for more convenient ways of writing to the window.

See also

AppendText


XYToPosition(x, y)

Converts the given zero based column and line number to a position.

Parameters:

  • x (long): The column number.
  • y (long): The line number.

Returns:

long


write(text)

Parameters:

  • text (string)

Properties

DefaultStyle
See GetDefaultStyle and SetDefaultStyle
InsertionPoint
See GetInsertionPoint and SetInsertionPoint
LastPosition
See GetLastPosition
NumberOfLines
See GetNumberOfLines
Selection
See GetSelection and SetSelection
StringSelection
See GetStringSelection
Value
See GetValue and SetValue