9.9. Text Entries
Prev | Chapter 9. Miscellaneous Widgets | Next

## 9.9. Text Entries

The `Entry` widget allows text to be typed and displayed in a single line text box. The text may be set with method calls that allow new text to replace, prepend or append the current contents of the `Entry` widget.

The function for creating an `Entry` widget is:

      entry = gtk.Entry(max=0)

If the `max` argument is given it sets a limit on the length of the text within the `Entry`. If `max` is 0 then there is no limit.

The maximum length of the entry can be changed using the method:

      entry.set_max_length(max)

The next method alters the text which is currently within the `Entry` widget.

      entry.set_text(text)

The `set_text`() method sets the contents of the `Entry` widget to `text`, replacing the current contents. Note that the class `Entry` implements the `Editable` interface (yes, `gobject` supports Java-like interfaces) which contains some more functions for manipulating the contents. For example, the method:

      entry.insert_text(text, position=0)

inserts `text` at the given position within the `entry`.

The contents of the `Entry` can be retrieved by using a call to the following method. This is useful in the callback methods described below.

      text = entry.get_text()

If we don't want the contents of the `Entry` to be changed by someone typing into it, we can change its editable state.

      entry.set_editable(is_editable)

The above method allows us to toggle the editable state of the `Entry` widget by passing in a `TRUE` or `FALSE` value for the `is_editable` argument.

If we are using the `Entry` where we don't want the text entered to be visible, for example when a password is being entered, we can use the following method, which also takes a boolean flag.

      entry.set_visibility(visible)

A region of the text may be set as selected by using the following method. This would most often be used after setting some default text in an `Entry`, making it easy for the user to remove it.

      entry.select_region(start, end)

If we want to be notified when the user has entered text, we can connect to the "activate" or "changed" signal. Activate is raised when the user hits the enter key within the `Entry` widget. Changed is raised when the any change is made to the text, e.g. for every character entered or removed.

The entry.py example program illustrates the use of an `Entry` widget. Figure 9.10, “Entry Example” shows the result of running the program:

Figure 9.10. Entry Example

The entry.py source code is:

        1	#!/usr/bin/env python
        2
        3	# example entry.py
        4
        5	import pygtk
        6	pygtk.require('2.0')
        7	import gtk
        8
        9	class EntryExample:
       10	    def enter_callback(self, widget, entry):
       11	        entry_text = entry.get_text()
       12	        print "Entry contents: %s\n" % entry_text
       13
       14	    def entry_toggle_editable(self, checkbutton, entry):
       15	        entry.set_editable(checkbutton.get_active())
       16
       17	    def entry_toggle_visibility(self, checkbutton, entry):
       18	        entry.set_visibility(checkbutton.get_active())
       19
       20	    def __init__(self):
       21	        # create a new window
       22	        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
       23	        window.set_size_request(200, 100)
       24	        window.set_title("GTK Entry")
       25	        window.connect("delete_event", lambda w,e: gtk.main_quit())
       26
       27	        vbox = gtk.VBox(False, 0)
       28	        window.add(vbox)
       29	        vbox.show()
       30
       31	        entry = gtk.Entry()
       32	        entry.set_max_length(50)
       33	        entry.connect("activate", self.enter_callback, entry)
       34	        entry.set_text("hello")
       35	        entry.insert_text(" world", len(entry.get_text()))
       36	        entry.select_region(0, len(entry.get_text()))
       37	        vbox.pack_start(entry, True, True, 0)
       38	        entry.show()
       39
       40	        hbox = gtk.HBox(False, 0)
       41	        vbox.add(hbox)
       42	        hbox.show()
       43
       44	        check = gtk.CheckButton("Editable")
       45	        hbox.pack_start(check, True, True, 0)
       46	        check.connect("toggled", self.entry_toggle_editable, entry)
       47	        check.set_active(True)
       48	        check.show()
       49
       50	        check = gtk.CheckButton("Visible")
       51	        hbox.pack_start(check, True, True, 0)
       52	        check.connect("toggled", self.entry_toggle_visibility, entry)
       53	        check.set_active(True)
       54	        check.show()
       55
       56	        button = gtk.Button(stock=gtk.STOCK_CLOSE)
       57	        button.connect("clicked", lambda w: gtk.main_quit())
       58	        vbox.pack_start(button, True, True, 0)
       59	        button.set_flags(gtk.CAN_DEFAULT)
       60	        button.grab_default()
       61	        button.show()
       62	        window.show()
       63
       64	def main():
       65	    gtk.main()
       66	    return 0
       67
       68	if __name__ == "__main__":
       69	    EntryExample()
       70	    main()

Prev | Up | Next
9.8. Statusbars | Home | 9.10. Spin Buttons