Inheritance diagram for wx.TextCtrl:
A text control allows text to be displayed and edited. It may be single line or multi-line.
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")
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.
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. |
Constructor, creating and showing a text control.
Parameters:
Returns:
Appends the text to the end of the text control.
Parameters:
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
Returns True if the selection can be copied to the clipboard.
Returns:
bool
Returns True if the selection can be cut to the clipboard.
Returns:
bool
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
Returns True if there is a redo facility available and the last operation can be redone.
Returns:
bool
Returns True if there is an undo facility available and the last operation can be undone.
Returns:
bool
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:
Note
Note that this function will not generate the wx.wxEVT_COMMAND_TEXT_UPDATED event. This is the only difference with SetValue.
Note
Note that this function will generate a wx.wxEVT_COMMAND_TEXT_UPDATED 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.
Returns the style currently used for the new text.
Returns:
See also
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
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
Gets the length of the specified line, not including any trailing newline character(s).
Parameters:
Returns:
int
Returns the contents of a given line in the text control, not including any trailing newline character(s).
Parameters:
Returns:
string
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.
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:
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.
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.
Parameters:
Returns:
string
Gets the text currently selected in the control. If there is no selection, the returned string is empty.
Returns:
string
Returns the style at this position in the text control. Not all platforms support this function.
Parameters:
Returns:
bool
See also
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
Find the character position in the text coresponding to the point given in pixels.
Possible return values for result are:
Parameters:
Returns:
(result, col, row)
Note
pt is in device coords but is not adjusted for the client area origin nor scrolling.
Find the character position in the text coresponding to the point given in pixels.
Possible return values for result are:
Parameters:
Returns:
(result, position)
Note
pt is in device coords but is not adjusted for the client area origin nor scrolling.
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
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
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
Returns True if this is a multi line edit control and False otherwise.
Returns:
bool
See also
Returns True if this is a single line edit control and False otherwise.
Returns:
bool
See also
Loads and displays the named file, if it exists.
Returns True if successful, False otherwise.
Parameters:
Returns:
bool
Parameters:
See also
Converts given position to a zero-based column, line number pair.
Parameters:
Returns:
(x, y)
See also
Removes the text starting at the first given position up to (but not including) the character at the last position.
Parameters:
Replaces the text starting at the first position up to (but not including) the character at the last position with the given text.
Parameters:
Saves the contents of the control in a text file.
Returns True if successful, False otherwise.
Parameters:
Returns:
bool
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
Makes the text item editable or read-only, overriding the wx.TE_READONLY flag.
Parameters:
See also
Sets the insertion point at the given position.
Parameters:
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.
Marks the control as being modified by the user or not.
Parameters:
See also
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:
Changes the style of the given range. If any attribute within style is not set, the corresponding attribute from GetDefaultStyle is used.
Parameters:
Returns:
bool
See also
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:
Note
Note that this function will generate a wx.wxEVT_COMMAND_TEXT_UPDATED event.
See also
Parameters:
Returns:
bool
Makes the line containing the given position visible.
Parameters:
Writes the text into the text control at the current insertion position.
Parameters:
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
Converts the given zero based column and line number to a position.
Parameters:
Returns:
long
Parameters: