A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://wxpython.org/Phoenix/docs/html/wx.TextCtrl.html below:

wx.TextCtrl — wxPython Phoenix 4.2.4a1 documentation

wx.TextCtrl¶

A text control allows text to be displayed and edited.

It may be single line or multi-line. Notice that a lot of methods of the text controls are found in the base wx.TextEntry class which is a common base class for wx.TextCtrl and other controls using a single line text entry field (e.g. wx.ComboBox).

Window Styles¶

This class supports the following styles:

Note that alignment styles (wx``wx.TE_LEFT``, wx.TE_CENTRE and wx.TE_RIGHT) can be changed dynamically after control creation on wxMSW, wxGTK and wxOSX. 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.

TextCtrl 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). 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. This problem doesn’t arise for single-line platforms however where the indices in the control do correspond to the positions in the value string.

TextCtrl Positions and Coordinates¶

It is possible to use either linear positions, i.e. roughly (but not always exactly, as explained in the previous section) the index of the character in the text contained in the control or X-Y coordinates, i.e. column and line of the character when working with this class and it provides the functions PositionToXY and XYToPosition to convert between the two. Additionally, a position in the control can be converted to its coordinates in pixels using PositionToCoords which can be useful to e.g. show a popup menu near the given character. And, in the other direction, wx.HitTest can be used to find the character under, or near, the given pixel coordinates. To be more precise, positions actually refer to the gaps between characters and not the characters themselves. Thus, position 0 is the one before the very first character in the control and so is a valid position even when the control is empty. And if the control contains a single character, it has two valid positions: 0 before this character and 1 – after it. This, when the documentation of various functions mentions “invalid position”, it doesn’t consider the position just after the last character of the line to be invalid, only the positions beyond that one (e.g. 2 and greater in the single character example) are actually invalid.

TextCtrl Styles¶

Multi-line text controls support styling, i.e. provide a possibility to set colours and font for individual characters in it (note that under Windows 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")
TextCtrl and C++ Streams¶

This class multiply-inherits from std::streambuf (except for some really old compilers using non-standard iostream library), allowing code such as the following:

# C++-style stream support is not implemented for Python.

Note that even if your build of wxWidgets doesn’t support this (the symbol HAS_TEXT_WINDOW_STREAM has value of 0 then) you can still use wx.TextCtrl itself in a stream-like manner:

# C++-style stream support is not implemented for Python.

However the possibility to create a std::ostream associated with wx.TextCtrl may be useful if you need to redirect the output of a function taking a std::ostream as parameter to a text control. Another commonly requested need is to redirect std::cout to the text control. This may be done in the following way:

# C++-style stream support is not implemented for Python.

But wxWidgets provides a convenient class to make it even simpler so instead you may just do

# C++-style stream support is not implemented for Python.

See StreamToTextRedirector for more details.

Event Handling¶

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

Events Emitted by this Class¶

Handlers bound for the following event types will receive one of the wx.CommandEvent parameters.

Class Hierarchy¶

Inheritance diagram for class

TextCtrl

:

Control Appearance¶ Methods Summary¶

__init__

Default constructor.

Create

Creates the text control for two-step construction.

DiscardEdits

Resets the internal modified flag as if the current changes had been saved.

EmptyUndoBuffer

Delete the undo history.

EmulateKeyPress

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

GetClassDefaultAttributes

GetDefaultStyle

Returns the style currently used for the new text.

GetLineLength

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

GetLineText

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

GetNumberOfLines

Returns the number of lines in the text control buffer.

GetStyle

Returns the style at this position in the text control.

HideNativeCaret

Turn off the widget’s native caret on Windows.

HitTestPos

Finds the position of the character at the specified point.

HitTest

Finds the row and column of the character at the specified point.

IsModified

Returns True if the text has been modified by user.

IsMultiLine

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

IsSingleLine

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

LoadFile

Loads and displays the named file, if it exists.

MacCheckSpelling

Turn on the native spell checking for the text widget on

MarkDirty

Mark text as modified (dirty).

OSXDisableAllSmartSubstitutions

Disables all automatic character substitutions.

OSXEnableAutomaticDashSubstitution

Enables the automatic conversion of two ASCII hyphens into an em dash.

OSXEnableAutomaticQuoteSubstitution

Enables the automatic replacement of straight (ASCII) quotation marks and apostrophes with smart (“curly”) quotes.

OSXEnableNewLineReplacement

Enables the automatic replacement of new lines characters in a single-line text field with spaces under macOS.

PositionToCoords

Converts given text position to client coordinates in pixels.

PositionToXY

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

SaveFile

Saves the contents of the control in a text file.

SetDefaultStyle

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

SetModified

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

SetStyle

Changes the style of the given range.

ShowNativeCaret

Turn on the widget’s native caret on Windows.

ShowPosition

Makes the line containing the given position visible.

XYToPosition

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

flush

NOP, for file-like compatibility.

write

Append text to the textctrl, for file-like compatibility.

Properties Summary¶ Class API¶
class wx.TextCtrl(Control, TextEntry)¶

Possible constructors:

TextCtrl() -> None

TextCtrl(parent, id=ID_ANY, value='', pos=DefaultPosition,
         size=DefaultSize, style=0, validator=DefaultValidator,
         name=TextCtrlNameStr) -> None

A text control allows text to be displayed and edited.


Methods¶
__init__(self, *args, **kw)¶

Overloaded Implementations:

__init__ (self)

Default constructor.

Return type:

None

__init__ (self, parent, id=ID_ANY, value=’’, pos=DefaultPosition, size=DefaultSize, style=0, validator=DefaultValidator, name=TextCtrlNameStr)

Constructor, creating and showing a text control.

Parameters:
  • parent (wx.Window) – Parent window. Should not be None.

  • id (wx.WindowID) – Control identifier. A value of -1 denotes a default value.

  • value (string) – Default text value.

  • pos (wx.Point) – Text control position.

  • size (wx.Size) – Text control size.

  • style (long) – Window style. See wx.TextCtrl.

  • validator (wx.Validator) – Window validator.

  • name (string) – Window name.

Return type:

None

Note

The horizontal scrollbar (wx``wx.HSCROLL`` style flag) will only be created for multi-line text controls. Without a horizontal scrollbar, text lines that don’t fit in the control’s size will be wrapped (but no newline character is inserted). Single line controls don’t have a horizontal scrollbar, the text is automatically scrolled so that the insertion point is always visible.



Create(self, parent, id=ID_ANY, value='', pos=DefaultPosition, size=DefaultSize, style=0, validator=DefaultValidator, name=TextCtrlNameStr)¶

Creates the text control for two-step construction.

This method should be called if the default constructor was used for the control creation. Its parameters have the same meaning as for the non-default constructor.

Parameters:
Return type:

bool


DiscardEdits(self)¶

Resets the internal modified flag as if the current changes had been saved.

Return type:

None


EmptyUndoBuffer(self)¶

Delete the undo history.

Currently only implemented in wxMSW (for controls using wx.TE_RICH2 style only) and wxOSX (for multiline text controls only), does nothing in the other ports or for the controls not using the appropriate styles.

Return type:

None

Added in version 4.1/wxWidgets-3.1.6.


EmulateKeyPress(self, event)¶

This function 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. Please note that this function doesn’t currently work correctly for all keys under any platform but MSW.

Parameters:

event (wx.KeyEvent)

Return type:

bool

Returns:

True if the event resulted in a change to the control, False otherwise.


static GetClassDefaultAttributes(variant=WINDOW_VARIANT_NORMAL)¶
Parameters:

variant (WindowVariant)

Return type:

wx.VisualAttributes


GetDefaultStyle(self)¶

Returns the style currently used for the new text.

Return type:

wx.TextAttr


GetLineLength(self, lineNo)¶

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

Parameters:

lineNo (long) – Line number (starting from zero).

Return type:

int

Returns:

The length of the line, or -1 if lineNo was invalid.


GetLineText(self, 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.

Return type:

str

Returns:

The contents of the line.


GetNumberOfLines(self)¶

Returns the number of lines in the text control buffer.

The returned number is the number of logical lines, i.e. just the count of the number of newline characters in the control + 1, for wxGTK and OSX/Cocoa ports while it is the number of physical lines, i.e. the count of lines actually shown in the control, in wxMSW. Because of this discrepancy, it is not recommended to use this function.

Return type:

int

Note

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


GetStyle(self, position, style)¶

Returns the style at this position in the text control.

Not all platforms support this function.

Parameters:
Return type:

bool

Returns:

True on success, False if an error occurred (this may also mean that the styles are not supported under this platform).


HideNativeCaret(self)¶

Turn off the widget’s native caret on Windows. Ignored on other platforms.

Return type:

bool


HitTestPos(self, pt)¶

Finds the position of the character at the specified point.

If the return code is not TE_HT_UNKNOWN the position of the character closest to this position is returned, otherwise the output parameter is not modified.

Please note that this function is currently only implemented in Univ, wxMSW and wxGTK ports and always returns TE_HT_UNKNOWN in the other ports.


HitTest(self, pt)¶

Finds the row and column of the character at the specified point.

If the return code is not TE_HT_UNKNOWN the row and column of the character closest to this position are returned, otherwise the output parameters are not modified.

Please note that this function is currently only implemented in Univ, wxMSW and wxGTK ports and always returns TE_HT_UNKNOWN in the other ports.


IsModified(self)¶

Returns True if the text has been modified by user.

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

Return type:

bool


IsMultiLine(self)¶

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

Return type:

bool


IsSingleLine(self)¶

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

Return type:

bool


LoadFile(self, filename, fileType=TEXT_TYPE_ANY)¶

Loads and displays the named file, if it exists.

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.

Return type:

bool

Returns:

True if successful, False otherwise.


MacCheckSpelling(self, check)¶

Turn on the native spell checking for the text widget on OSX. Ignored on other platforms.

Return type:

None


MarkDirty(self)¶

Mark text as modified (dirty).

Return type:

None


OSXDisableAllSmartSubstitutions(self)¶

Disables all automatic character substitutions.

Return type:

None

Added in version 4.1/wxWidgets-3.1.1.

Availability

Only available for OSX.


OSXEnableAutomaticDashSubstitution(self, enable)¶

Enables the automatic conversion of two ASCII hyphens into an em dash.

This feature is enabled by default.

Parameters:

enable (bool)

Return type:

None

Added in version 4.1/wxWidgets-3.1.1.

Availability

Only available for OSX.


OSXEnableAutomaticQuoteSubstitution(self, enable)¶

Enables the automatic replacement of straight (ASCII) quotation marks and apostrophes with smart (“curly”) quotes.

This feature is enabled by default.

Parameters:

enable (bool)

Return type:

None

Added in version 4.1/wxWidgets-3.1.1.

Availability

Only available for OSX.


OSXEnableNewLineReplacement(self, enable)¶

Enables the automatic replacement of new lines characters in a single-line text field with spaces under macOS.

This feature is enabled by default and will replace any new line ( \n ) character entered into a single-line text field with the space character. Usually single-line text fields are not expected to hold multiple lines of text (that is what wx.TE_MULTILINE is for, after all) and it is impossible to have multiple lines of text in them under non-Mac platforms. However, under macOS/Cocoa, a single-line text control can still show multiple lines and this function allows to lift the restriction preventing multiple lines from being entered unless wx.TE_MULTILINE is specified.

Parameters:

enable (bool)

Return type:

None

Added in version 4.1/wxWidgets-3.1.6.

Availability

Only available for OSX.

Note

Has no effect if the wx.TE_MULTILINE flag is set on a text control.


PositionToCoords(self, pos)¶

Converts given text position to client coordinates in pixels.

This function allows finding where is the character at the given position displayed in the text control.

Parameters:

pos (long) – Text position in 0 to GetLastPosition range (inclusive).

Return type:

wx.Point

Returns:

On success returns a wx.Point which contains client coordinates for the given position in pixels, otherwise returns wx.DefaultPosition .

Added in version 2.9.3.

Availability

Only available for MSW, GTK . Additionally, wxGTK only implements this method for multiline controls and wx.DefaultPosition is always returned for the single line ones.


PositionToXY(self, pos)¶

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

Parameters:

pos (long) – Position.

Return type:

Tuple[bool, int, int]


SaveFile(self, filename='', fileType=TEXT_TYPE_ANY)¶

Saves the contents of the control in a text file.

Parameters:
  • filename (string) – The name of the file in which to save the text.

  • fileType (int) – The type of file to save. This is currently ignored in wx.TextCtrl.

Return type:

bool

Returns:

True if the operation was successful, False otherwise.


SetDefaultStyle(self, style)¶

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

This applies both to the text added programmatically using WriteText or AppendText and to the text entered by the user interactively.

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 either, 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).

Parameters:

style (wx.TextAttr) – The style for the new text.

Return type:

bool

Returns:

True on success, False if an error occurred (this may also mean that the styles are not supported under this platform).


SetModified(self, modified)¶

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

Parameters:

modified (bool)

Return type:

None


SetStyle(self, 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.

Return type:

bool

Returns:

True on success, False if an error occurred (this may also mean that the styles are not supported under this platform).


ShowNativeCaret(self, show=True)¶

Turn on the widget’s native caret on Windows. Ignored on other platforms.

Return type:

bool


ShowPosition(self, pos)¶

Makes the line containing the given position visible.

Parameters:

pos (long) – The position that should be visible.

Return type:

None


XYToPosition(self, 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.

Return type:

int

Returns:

The position value, or -1 if x or y was invalid.


flush(self)¶

NOP, for file-like compatibility.

Return type:

None


write(self, text)¶

Append text to the textctrl, for file-like compatibility.

Return type:

None


Properties¶
DefaultStyle¶

See GetDefaultStyle and SetDefaultStyle


NumberOfLines¶

See GetNumberOfLines


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4