8. Entry — Python GTK+ 3 Tutorial 3.4 documentation (original) (raw)
Entry widgets allow the user to enter text. You can change the contents with theGtk.Entry.set_text() method, and read the current contents with theGtk.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() withFalse
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, useGtk.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_icon_name()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, useGtk.Entry.set_icon_tooltip_text() or the corresponding function for markup.
8.1. Example
1import gi 2 3gi.require_version("Gtk", "3.0") 4from gi.repository import Gtk, GLib 5 6 7class EntryWindow(Gtk.Window): 8 def init(self): 9 super().init(title="Entry Demo") 10 self.set_size_request(200, 100) 11 12 self.timeout_id = None 13 14 vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) 15 self.add(vbox) 16 17 self.entry = Gtk.Entry() 18 self.entry.set_text("Hello World") 19 vbox.pack_start(self.entry, True, True, 0) 20 21 hbox = Gtk.Box(spacing=6) 22 vbox.pack_start(hbox, True, True, 0) 23 24 self.check_editable = Gtk.CheckButton(label="Editable") 25 self.check_editable.connect("toggled", self.on_editable_toggled) 26 self.check_editable.set_active(True) 27 hbox.pack_start(self.check_editable, True, True, 0) 28 29 self.check_visible = Gtk.CheckButton(label="Visible") 30 self.check_visible.connect("toggled", self.on_visible_toggled) 31 self.check_visible.set_active(True) 32 hbox.pack_start(self.check_visible, True, True, 0) 33 34 self.pulse = Gtk.CheckButton(label="Pulse") 35 self.pulse.connect("toggled", self.on_pulse_toggled) 36 self.pulse.set_active(False) 37 hbox.pack_start(self.pulse, True, True, 0) 38 39 self.icon = Gtk.CheckButton(label="Icon") 40 self.icon.connect("toggled", self.on_icon_toggled) 41 self.icon.set_active(False) 42 hbox.pack_start(self.icon, True, True, 0) 43 44 def on_editable_toggled(self, button): 45 value = button.get_active() 46 self.entry.set_editable(value) 47 48 def on_visible_toggled(self, button): 49 value = button.get_active() 50 self.entry.set_visibility(value) 51 52 def on_pulse_toggled(self, button): 53 if button.get_active(): 54 self.entry.set_progress_pulse_step(0.2) 55 # Call self.do_pulse every 100 ms 56 self.timeout_id = GLib.timeout_add(100, self.do_pulse, None) 57 else: 58 # Don't call self.do_pulse anymore 59 GLib.source_remove(self.timeout_id) 60 self.timeout_id = None 61 self.entry.set_progress_pulse_step(0) 62 63 def do_pulse(self, user_data): 64 self.entry.progress_pulse() 65 return True 66 67 def on_icon_toggled(self, button): 68 if button.get_active(): 69 icon_name = "system-search-symbolic" 70 else: 71 icon_name = None 72 self.entry.set_icon_from_icon_name(Gtk.EntryIconPosition.PRIMARY, icon_name) 73 74 75win = EntryWindow() 76win.connect("destroy", Gtk.main_quit) 77win.show_all() 78Gtk.main()