Gtk.Entry¶
class — extends Widget, Accessible, Buildable, CellEditable, ConstraintTarget, Editable
A single-line text entry widget.
<picture> <source srcset="entry-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkEntry" src="entry.png"> </picture>
A fairly large set of key bindings are supported by default. If the entered text is longer than the allocation of the widget, the widget will scroll so that the cursor position is visible.
When using an entry for passwords and other sensitive information, it
can be put into “password mode” using Entry.set_visibility.
In this mode, entered text is displayed using a “invisible” character.
By default, GTK picks the best invisible character that is available
in the current font, but it can be changed with
Entry.set_invisible_char.
GtkEntry has the ability to display progress or activity
information behind the text. To make an entry display such information,
use Entry.set_progress_fraction or
Entry.set_progress_pulse_step.
Additionally, GtkEntry can show icons at either side of the entry.
These icons can be activatable by clicking, can be set up as drag source
and can have tooltips. To add an icon, use
Entry.set_icon_from_gicon or one of the various other functions
that set an icon from an icon name or a paintable. To trigger an action when
the user clicks an icon, connect to the Entry.icon-press signal.
To allow DND operations from an icon, use
Entry.set_icon_drag_source. To set a tooltip on an icon, use
Entry.set_icon_tooltip_text or the corresponding function
for markup.
Note that functionality or information that is only available by clicking on an icon in an entry may not be accessible at all to users which are not able to use a mouse or other pointing device. It is therefore recommended that any such functionality should also be available by other means, e.g. via the context menu of the entry.
CSS nodes¶
entry[.flat][.warning][.error]
├── text[.readonly]
├── image.left
├── image.right
╰── [progress[.pulse]]
GtkEntry has a main node with the name entry. Depending on the properties
of the entry, the style classes .read-only and .flat may appear. The style
classes .warning and .error may also be used with entries.
When the entry shows icons, it adds subnodes with the name image and the style class .left or .right, depending on where the icon appears.
When the entry shows progress, it adds a subnode with the name progress. The node has the style class .pulse when the shown progress is pulsing.
For all the subnodes added to the text node in various situations,
see Text.
GtkEntry as GtkBuildable¶
The GtkEntry implementation of the GtkBuildable interface supports a
custom <attributes> element, which supports any number of <attribute>
elements. The <attribute> element has attributes named “name“, “value“,
“start“ and “end“ and allows you to specify PangoAttribute values for
this label.
An example of a UI definition fragment specifying Pango attributes:
<object class="GtkEntry">
<attributes>
<attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
<attribute name="background" value="red" start="5" end="10"/>
</attributes>
</object>
The start and end attributes specify the range of characters to which the Pango attribute applies. If start and end are not specified, the attribute is applied to the whole text. Note that specifying ranges does not make much sense with translatable attributes. Use markup embedded in the translatable content instead.
Accessibility¶
GtkEntry uses the AccessibleRole.text_box role.
Constructors¶
new¶
Creates a new entry.
new_with_buffer¶
Creates a new entry with the specified text buffer.
Parameters:
buffer— The buffer to use for the newGtkEntry.
Methods¶
get_activates_default¶
Retrieves the value set by Entry.set_activates_default.
get_alignment¶
Gets the value set by Entry.set_alignment.
See also: Editable.xalign
get_attributes¶
Gets the attribute list of the GtkEntry.
See Entry.set_attributes.
get_buffer¶
Get the GtkEntryBuffer object which holds the text for
this widget.
get_completion¶
:::warning Deprecated since 4.10 This API is deprecated. :::
Returns the auxiliary completion object currently
in use by entry.
get_current_icon_drag_source¶
Returns the index of the icon which is the source of the current DND operation, or -1.
get_extra_menu¶
Gets the menu model set with Entry.set_extra_menu.
get_has_frame¶
Gets the value set by Entry.set_has_frame.
get_icon_activatable¶
Returns whether the icon is activatable.
Parameters:
icon_pos— Icon position
get_icon_area¶
Gets the area where entry’s icon at icon_pos is drawn.
This function is useful when drawing something to the entry in a draw callback.
If the entry is not realized or has no icon at the given
position, icon_area is filled with zeros. Otherwise,
icon_area will be filled with the icon's allocation,
relative to entry's allocation.
Parameters:
icon_pos— Icon position
get_icon_at_pos¶
Finds the icon at the given position and return its index.
The position’s coordinates are relative to the entry’s
top left corner. If x, y doesn’t lie inside an icon,
-1 is returned. This function is intended for use in a
Widget.query-tooltip signal handler.
Parameters:
x— the x coordinate of the position to find, relative toentryy— the y coordinate of the position to find, relative toentry
get_icon_gicon¶
Retrieves the GIcon used for the icon.
None will be returned if there is no icon or if the icon was
set by some other method (e.g., by GdkPaintable or icon name).
Parameters:
icon_pos— Icon position
get_icon_name¶
Retrieves the icon name used for the icon.
None is returned if there is no icon or if the icon was set
by some other method (e.g., by GdkPaintable or gicon).
Parameters:
icon_pos— Icon position
get_icon_paintable¶
Retrieves the GdkPaintable used for the icon.
If no GdkPaintable was used for the icon, None is returned.
Parameters:
icon_pos— Icon position
get_icon_sensitive¶
Returns whether the icon appears sensitive or insensitive.
Parameters:
icon_pos— Icon position
get_icon_storage_type¶
Gets the type of representation being used by the icon to store image data.
If the icon has no image data, the return value will
be ImageType.EMPTY.
Parameters:
icon_pos— Icon position
get_icon_tooltip_markup¶
Gets the contents of the tooltip on the icon at the specified
position in entry.
Parameters:
icon_pos— the icon position
get_icon_tooltip_text¶
Gets the contents of the tooltip on the icon at the specified
position in entry.
Parameters:
icon_pos— the icon position
get_input_hints¶
Gets the input hints of this GtkEntry.
get_input_purpose¶
Gets the input purpose of the GtkEntry.
get_invisible_char¶
Retrieves the character displayed in place of the actual text in “password mode”.
get_max_length¶
Retrieves the maximum allowed length of the text in entry.
See Entry.set_max_length.
get_menu_entry_icon_text¶
Gets the text that will be used in the context menu of the GtkEntry
when the specified icon is activatable. Selecting this item in the menu
results, from all aspects, the same than clicking on the specified icon.
This greatly simplifies making accessible applications, because the icons
aren't focusable when using keyboard navigation. This is why Gtk recommends
to add the same action to the context menu.
Parameters:
icon_pos— eitherGTK_ENTRY_ICON_PRIMARYorGTK_ENTRY_ICON_SECONDARY
get_overwrite_mode¶
Gets whether the GtkEntry is in overwrite mode.
get_placeholder_text¶
Retrieves the text that will be displayed when entry
is empty and unfocused
get_progress_fraction¶
Returns the current fraction of the task that’s been completed.
See Entry.set_progress_fraction.
get_progress_pulse_step¶
Retrieves the pulse step set with
Entry.set_progress_pulse_step.
get_tabs¶
Gets the tabstops of the GtkEntry.
See Entry.set_tabs.
get_text_length¶
Retrieves the current length of the text in entry.
This is equivalent to getting entry's GtkEntryBuffer
and calling EntryBuffer.get_length on it.
get_visibility¶
Retrieves whether the text in entry is visible.
See Entry.set_visibility.
grab_focus_without_selecting¶
Causes entry to have keyboard focus.
It behaves like Widget.grab_focus, except that it doesn't
select the contents of the entry. You only want to call this on some
special entries which the user usually doesn't want to replace all text
in, such as search-as-you-type entries.
progress_pulse¶
Indicates that some progress is made, but you don’t know how much.
Causes the entry’s progress indicator to enter “activity
mode”, where a block bounces back and forth. Each call to
Entry.progress_pulse causes the block to move by a
little bit (the amount of movement per pulse is determined
by Entry.set_progress_pulse_step).
reset_im_context¶
Reset the input method context of the entry if needed.
This can be necessary in the case where modifying the buffer would confuse on-going input method behavior.
set_activates_default¶
Sets whether pressing Enter in the entry will activate the default
widget for the window containing the entry.
This usually means that the dialog containing the entry will be closed, since the default widget is usually one of the dialog buttons.
Parameters:
setting—Trueto activate window’s default widget on Enter keypress
set_alignment¶
Sets the alignment for the contents of the entry.
This controls the horizontal positioning of the contents when the displayed text is shorter than the width of the entry.
See also: Editable.xalign
Parameters:
xalign— The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts
set_attributes¶
Sets a PangoAttrList.
The attributes in the list are applied to the entry text.
Since the attributes will be applied to text that changes as the user types, it makes most sense to use attributes with unlimited extent.
Parameters:
attrs— aPangoAttrList
set_buffer¶
Set the GtkEntryBuffer object which holds the text for
this widget.
Parameters:
buffer— aGtkEntryBuffer
set_completion¶
:::warning Deprecated since 4.10 This API is deprecated. :::
Sets completion to be the auxiliary completion object
to use with entry.
All further configuration of the completion mechanism is
done on completion using the GtkEntryCompletion API.
Completion is disabled if completion is set to None.
Parameters:
completion— TheGtkEntryCompletion
set_extra_menu¶
Sets a menu model to add when constructing
the context menu for entry.
Parameters:
model— aGMenuModel
set_has_frame¶
Sets whether the entry has a beveled frame around it.
Parameters:
setting— new value
set_icon_activatable¶
Sets whether the icon is activatable.
Parameters:
icon_pos— Icon positionactivatable—Trueif the icon should be activatable
set_icon_drag_source¶
def set_icon_drag_source(self, icon_pos: EntryIconPosition | int, provider: Gdk.ContentProvider, actions: Gdk.DragAction | int) -> None
Sets up the icon at the given position as drag source.
This makes it so that GTK will start a drag operation when the user clicks and drags the icon.
Parameters:
icon_pos— icon positionprovider— aGdkContentProvideractions— a bitmask of the allowed drag actions
set_icon_from_gicon¶
def set_icon_from_gicon(self, icon_pos: EntryIconPosition | int, icon: Gio.Icon | None = ...) -> None
Sets the icon shown in the entry at the specified position from the current icon theme.
If the icon isn’t known, a “broken image” icon will be displayed instead.
If icon is None, no icon will be shown in the
specified position.
Parameters:
icon_pos— The position at which to set the iconicon— The icon to set
set_icon_from_icon_name¶
def set_icon_from_icon_name(self, icon_pos: EntryIconPosition | int, icon_name: str | None = ...) -> None
Sets the icon shown in the entry at the specified position from the current icon theme.
If the icon name isn’t known, a “broken image” icon will be displayed instead.
If icon_name is None, no icon will be shown in the
specified position.
Parameters:
icon_pos— The position at which to set the iconicon_name— An icon name
set_icon_from_paintable¶
def set_icon_from_paintable(self, icon_pos: EntryIconPosition | int, paintable: Gdk.Paintable | None = ...) -> None
Sets the icon shown in the specified position using a GdkPaintable.
If paintable is None, no icon will be shown in the specified position.
Parameters:
icon_pos— Icon positionpaintable— AGdkPaintable
set_icon_sensitive¶
Sets the sensitivity for the specified icon.
Parameters:
icon_pos— Icon positionsensitive— Specifies whether the icon should appear sensitive or insensitive
set_icon_tooltip_markup¶
def set_icon_tooltip_markup(self, icon_pos: EntryIconPosition | int, tooltip: str | None = ...) -> None
Sets tooltip as the contents of the tooltip for the icon at
the specified position.
tooltip is assumed to be marked up with Pango Markup.
Use None for tooltip to remove an existing tooltip.
See also Widget.set_tooltip_markup and
Entry.set_icon_tooltip_text.
Parameters:
icon_pos— the icon positiontooltip— the contents of the tooltip for the icon
set_icon_tooltip_text¶
def set_icon_tooltip_text(self, icon_pos: EntryIconPosition | int, tooltip: str | None = ...) -> None
Sets tooltip as the contents of the tooltip for the icon
at the specified position.
Use None for tooltip to remove an existing tooltip.
See also Widget.set_tooltip_text and
Entry.set_icon_tooltip_markup.
If you unset the widget tooltip via
Widget.set_tooltip_text or
Widget.set_tooltip_markup, this sets
Widget.has-tooltip to False, which suppresses
icon tooltips too. You can resolve this by then calling
Widget.set_has_tooltip to set
Widget.has-tooltip back to True, or
setting at least one non-empty tooltip on any icon
achieves the same result.
Parameters:
icon_pos— the icon positiontooltip— the contents of the tooltip for the icon
set_input_hints¶
Set additional hints which allow input methods to fine-tune their behavior.
Parameters:
hints— the hints
set_input_purpose¶
Sets the input purpose which can be used by input methods to adjust their behavior.
Parameters:
purpose— the purpose
set_invisible_char¶
Sets the character to use in place of the actual text in “password mode”.
See Entry.set_visibility for how to enable
“password mode”.
By default, GTK picks the best invisible char available in the current font. If you set the invisible char to 0, then the user will get no feedback at all; there will be no text on the screen as they type.
Parameters:
ch— a Unicode character
set_max_length¶
Sets the maximum allowed length of the contents of the widget.
If the current contents are longer than the given length, then they will be truncated to fit. The length is in characters.
This is equivalent to getting entry's GtkEntryBuffer and
calling EntryBuffer.set_max_length on it.
Parameters:
max— the maximum length of the entry, or 0 for no maximum. (other than the maximum length of entries.) The value passed in will be clamped to the range 0-65536.
set_menu_entry_icon_text¶
Sets the text that will be used in the context menu of the GtkEntry
when the specified icon is activatable. Selecting this item in the menu
results, from all aspects, the same than clicking on the specified icon.
This greatly simplifies making accessible applications, because the icons
aren't focusable when using keyboard navigation. This is why Gtk recommends
to add the same action to the context menu.
Parameters:
icon_pos— eitherGTK_ENTRY_ICON_PRIMARYorGTK_ENTRY_ICON_SECONDARYtext— the text used for the menu item in the context menu, or NULL to not add a menu item.
set_overwrite_mode¶
Sets whether the text is overwritten when typing in the GtkEntry.
Parameters:
overwrite— new value
set_placeholder_text¶
Sets text to be displayed in entry when it is empty.
This can be used to give a visual hint of the expected
contents of the GtkEntry.
Parameters:
text— a string to be displayed whenentryis empty and unfocused
set_progress_fraction¶
Causes the entry’s progress indicator to “fill in” the given fraction of the bar.
The fraction should be between 0.0 and 1.0, inclusive.
Parameters:
fraction— fraction of the task that’s been completed
set_progress_pulse_step¶
Sets the fraction of total entry width to move the progress bouncing block for each pulse.
Use Entry.progress_pulse to pulse
the progress.
Parameters:
fraction— fraction between 0.0 and 1.0
set_tabs¶
Sets a PangoTabArray.
The tabstops in the array are applied to the entry text.
Parameters:
tabs— aPangoTabArray
set_visibility¶
Sets whether the contents of the entry are visible or not.
When visibility is set to False, characters are displayed
as the invisible char, and will also appear that way when
the text in the entry widget is copied elsewhere.
By default, GTK picks the best invisible character available
in the current font, but it can be changed with
Entry.set_invisible_char.
Note that you probably want to set Entry.input-purpose
to InputPurpose.PASSWORD or InputPurpose.PIN to
inform input methods about the purpose of this entry,
in addition to setting visibility to False.
Parameters:
visible—Trueif the contents of the entry are displayed as plaintext
unset_invisible_char¶
Unsets the invisible char, so that the default invisible char
is used again. See Entry.set_invisible_char.
Virtual methods¶
do_activate¶
Class handler for the GtkEntry::activate signal. The default
implementation activates the gtk.activate-default action.
Properties¶
activates_default¶
Whether to activate the default widget when Enter is pressed.
attributes¶
A list of Pango attributes to apply to the text of the entry.
This is mainly useful to change the size or weight of the text.
The PangoAttribute's start_index and end_index must refer to the
EntryBuffer text, i.e. without the preedit string.
buffer¶
The buffer object which actually stores the text.
completion¶
:::warning Deprecated since 4.10 This API is deprecated. :::
The auxiliary completion object to use with the entry.
enable_emoji_completion¶
Whether to suggest Emoji replacements for :-delimited names
like :heart:.
extra_menu¶
A menu model whose contents will be appended to the context menu.
has_frame¶
Whether the entry should draw a frame.
im_module¶
Which IM (input method) module should be used for this entry.
See IMContext.
Setting this to a non-None value overrides the system-wide IM
module setting. See the GtkSettings Settings.gtk-im-module
property.
input_hints¶
Additional hints that allow input methods to fine-tune their behavior.
Also see Entry.input-purpose
input_purpose¶
The purpose of this text field.
This property can be used by on-screen keyboards and other input methods to adjust their behaviour.
Note that setting the purpose to InputPurpose.PASSWORD or
InputPurpose.PIN is independent from setting
Entry.visibility.
invisible_char¶
The character to use when masking entry contents (“password mode”).
invisible_char_set¶
Whether the invisible char has been set for the GtkEntry.
max_length¶
Maximum number of characters for this entry.
menu_entry_icon_primary_text¶
Text for an item in the context menu to activate the primary icon action.
When the primary icon is activatable and this property has been set, a new entry in the context menu of this GtkEntry will appear with this text. Selecting that menu entry will result in the primary icon being activated, exactly in the same way as it would be activated from a mouse click.
This simplifies adding accessibility support to applications using activatable icons. The activatable icons aren't focusable when navigating the interface with the keyboard This is why Gtk recommends to also add those actions in the context menu. This set of methods greatly simplifies this, by adding a menu item that, when enabled, calls the same callback than clicking on the icon.
menu_entry_icon_secondary_text¶
Text for an item in the context menu to activate the secondary icon action.
When the primary icon is activatable and this property has been set, a new entry in the context menu of this GtkEntry will appear with this text. Selecting that menu entry will result in the primary icon being activated, exactly in the same way as it would be activated from a mouse click.
This simplifies adding accessibility support to applications using activatable icons. The activatable icons aren't focusable when navigating the interface with the keyboard This is why Gtk recommends to also add those actions in the context menu. This set of methods greatly simplifies this, by adding a menu item that, when enabled, calls the same callback than clicking on the icon.
overwrite_mode¶
If text is overwritten when typing in the GtkEntry.
placeholder_text¶
The text that will be displayed in the GtkEntry when it is empty
and unfocused.
primary_icon_activatable¶
Whether the primary icon is activatable.
GTK emits the Entry.icon-press and
Entry.icon-release signals only on sensitive,
activatable icons.
Sensitive, but non-activatable icons can be used for purely informational purposes.
primary_icon_gicon¶
The GIcon to use for the primary icon for the entry.
primary_icon_name¶
The icon name to use for the primary icon for the entry.
primary_icon_paintable¶
A GdkPaintable to use as the primary icon for the entry.
primary_icon_sensitive¶
Whether the primary icon is sensitive.
An insensitive icon appears grayed out. GTK does not emit the
Entry.icon-press and Entry.icon-release
signals and does not allow DND from insensitive icons.
An icon should be set insensitive if the action that would trigger when clicked is currently not available.
primary_icon_storage_type¶
The representation which is used for the primary icon of the entry.
primary_icon_tooltip_markup¶
The contents of the tooltip on the primary icon, with markup.
Also see Entry.set_icon_tooltip_markup.
primary_icon_tooltip_text¶
The contents of the tooltip on the primary icon.
Also see Entry.set_icon_tooltip_text.
progress_fraction¶
The current fraction of the task that's been completed.
progress_pulse_step¶
The fraction of total entry width to move the progress bouncing block for each pulse.
See Entry.progress_pulse.
scroll_offset¶
Number of pixels of the entry scrolled off the screen to the left.
secondary_icon_activatable¶
Whether the secondary icon is activatable.
GTK emits the Entry.icon-press and
Entry.icon-release signals only on sensitive,
activatable icons.
Sensitive, but non-activatable icons can be used for purely informational purposes.
secondary_icon_gicon¶
The GIcon to use for the secondary icon for the entry.
secondary_icon_name¶
The icon name to use for the secondary icon for the entry.
secondary_icon_paintable¶
A GdkPaintable to use as the secondary icon for the entry.
secondary_icon_sensitive¶
Whether the secondary icon is sensitive.
An insensitive icon appears grayed out. GTK does not emit the
[signalGtk.Entry::icon-press[ and Entry.icon-release
signals and does not allow DND from insensitive icons.
An icon should be set insensitive if the action that would trigger when clicked is currently not available.
secondary_icon_storage_type¶
The representation which is used for the secondary icon of the entry.
secondary_icon_tooltip_markup¶
The contents of the tooltip on the secondary icon, with markup.
Also see Entry.set_icon_tooltip_markup.
secondary_icon_tooltip_text¶
The contents of the tooltip on the secondary icon.
Also see Entry.set_icon_tooltip_text.
show_emoji_icon¶
Whether the entry will show an Emoji icon in the secondary icon position to open the Emoji chooser.
tabs¶
A list of tabstops to apply to the text of the entry.
text_length¶
The length of the text in the GtkEntry.
truncate_multiline¶
When True, pasted multi-line text is truncated to the first line.
visibility¶
Whether the entry should show the “invisible char” instead of the actual text (“password mode”).
Signals¶
activate¶
Emitted when the entry is activated.
The keybindings for this signal are all forms of the Enter key.
icon-press¶
Emitted when an activatable icon is clicked.
icon-release¶
Emitted on the button release from a mouse click over an activatable icon.