7. Entry

Entry widgets allow the user to enter text. You can change the contents with the Gtk.Entry.set_text() method, and read the current contents with the Gtk.Entry.get_text() method. You can also limit the number of characters the Entry can take by calling Gtk.Entry.set_max_length().

Occasionally you might want to make an Entry widget read-only. This can be done by passing False to the Gtk.Entry.set_editable() method.

Entry widgets can also be used to retrieve passwords from the user. It is common practice to hide the characters typed into the entry to prevent revealing the password to a third party. Calling Gtk.Entry.set_visibility() with False will cause the text to be hidden.

Gtk.Entry has the ability to display progress or activity information behind the text. This is similar to Gtk.ProgressBar widget and is commonly found in web browsers to indicate how much of a page download has been completed. To make an entry display such information, use Gtk.Entry.set_progress_fraction(), Gtk.Entry.set_progress_pulse_step(), or Gtk.Entry.progress_pulse().

Additionally, an Entry 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 Gtk.Entry.set_icon_from_stock() or one of the various other functions that set an icon from an icon name, a pixbuf, or icon theme. To set a tooltip on an icon, use Gtk.Entry.set_icon_tooltip_text() or the corresponding function for markup.

7.1. Entry Objects

class Gtk.Entry
get_text()

Retrieves the contents of the entry widget.

set_text(text)

Sets the text in the widget to the given value, replacing the current contents.

set_visibility(visible)

Sets whether the contents of the entry are visible or not. When visible 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.

set_max_length(max)

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.

set_editable(is_editable)

Determines if the user can edit the text in the editable widget or not. If is_editable is True, the user is allowed to edit the text in the widget.

set_progress_fraction(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.

set_progress_pulse_step(fraction)

Sets the fraction of total entry width to move the progress bouncing block for each call to progress_pulse().

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 progress_pulse() causes the block to move by a little bit (the amount of movement per pulse is determined by set_progress_pulse_step()).

set_icon_from_stock(icon_pos, stock_id)

Sets the icon shown in the entry at the specified position from a stock item. If stock_id is None, no icon will be shown in the specified position.

icon_pos specifies the side of the entry at which an icon is placed and can be one of

  • Gtk.EntryIconPosition.PRIMARY: At the beginning of the entry (depending on the text direction).
  • Gtk.EntryIconPosition.SECONDARY: At the end of the entry (depending on the text direction).
set_icon_tooltip_text(icon_pos, tooltip)

Sets tooltip as the contents of the tooltip for the icon at the specified position. If tooltip is None, an existing tooltip is removed.

For allowed values for icon_pos see set_icon_from_stock().

7.2. Example

_images/entry_example.png
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from gi.repository import Gtk, GObject

class EntryWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Entry Demo")
        self.set_size_request(200, 100)

        self.timeout_id = None

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(vbox)

        self.entry = Gtk.Entry()
        self.entry.set_text("Hello World")
        vbox.pack_start(self.entry, True, True, 0)

        hbox = Gtk.Box(spacing=6)
        vbox.pack_start(hbox, True, True, 0)
        
        self.check_editable = Gtk.CheckButton("Editable")
        self.check_editable.connect("toggled", self.on_editable_toggled)
        self.check_editable.set_active(True)
        hbox.pack_start(self.check_editable, True, True, 0)

        self.check_visible = Gtk.CheckButton("Visible")
        self.check_visible.connect("toggled", self.on_visible_toggled)
        self.check_visible.set_active(True)
        hbox.pack_start(self.check_visible, True, True, 0)

        self.pulse = Gtk.CheckButton("Pulse")
        self.pulse.connect("toggled", self.on_pulse_toggled)
        self.pulse.set_active(False)
        hbox.pack_start(self.pulse, True, True, 0)

        self.icon = Gtk.CheckButton("Icon")
        self.icon.connect("toggled", self.on_icon_toggled)
        self.icon.set_active(False)
        hbox.pack_start(self.icon, True, True, 0)

    def on_editable_toggled(self, button):
        value = button.get_active()
        self.entry.set_editable(value)

    def on_visible_toggled(self, button):
        value = button.get_active()
        self.entry.set_visibility(value)

    def on_pulse_toggled(self, button):
        if button.get_active():
            self.entry.set_progress_pulse_step(0.2)
            # Call self.do_pulse every 100 ms
            self.timeout_id = GObject.timeout_add(100, self.do_pulse, None)
        else:
            # Don't call self.do_pulse anymore
            GObject.source_remove(self.timeout_id)
            self.timeout_id = None
            self.entry.set_progress_pulse_step(0)

    def do_pulse(self, user_data):
        self.entry.progress_pulse()
        return True

    def on_icon_toggled(self, button):
        if button.get_active():
            stock_id = Gtk.STOCK_FIND
        else:
            stock_id = None
        self.entry.set_icon_from_stock(Gtk.EntryIconPosition.PRIMARY,
            stock_id)

win = EntryWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Project Versions

Table Of Contents

Previous topic

6. Label

Next topic

8. Button Widgets

This Page