Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login


Single Line Text Entry Field


As the title and the above image reveal a Gtk::Entry object or widget is a single line free-form text entry gadget. It can be used for text entry, password entry, and even number selections. As you will learn in the next session Gtk::SpinButton is derived from it. Gtk::Entry implements the Gtk::Editable interface containing methods for general text manipulation. It contains a large number of action signals used for key bindings, and several signals that an application can connect to so it can modify its behaviour. The additional functionality is needed to allow manipulation of selected (highlighted) text, delete and insert characters or strings (cut and paste), etc.

There are many properties you need to learn about in order to handle text with all bells and whistles. Here I will list just a few:

  • visibility
  • editable
  • invisible_char
  • text


You should consult the documentation (class Gtk::Entry - Properties) for full list.

#!/usr/bin/env ruby
require 'gtk2'

# etc/Etc are Unix/Linux feature if you do not have this feature,
# you should replace the [getlogin] below with something reasonable.
require 'etc'
include Etc

window =
window.set_title  "Password?"
window.border_width = 10
window.signal_connect('delete_event') { Gtk.main_quit }

# Note "getlogin" is Unix/Linux feature if you do not have it,
# you should replace the [getlogin] below with something reasonable.
question  ="What is %s's password?" % [getlogin])
entry_label ="Password:")

pass =
pass.visibility = false

# The following property takes integer value not string character
# pass.invisible_char = 42           ### for instance 42=asterisk

hbox =, 5)
vbox =, 5)


As you can see, we have turned our entry object into password widget by setting its "visibility" property. However I did not set my own "invisible_char" since the default on my system is quite acceptable (see the image above).

Following is a more elaborate example from the original Japanese tutorial page:

You can dynamically change entry properties. Of course you will understand the program better if you try it out.


#!/usr/bin/env ruby

require 'gtk2'

window =
window.set_size_request(200, 100)
window.title = "GTK Entry"
window.signal_connect("destroy") {Gtk.main_quit}

vbox =, 0)

entry =
entry.max_length = 50
entry.signal_connect("activate") {puts "Entry contents: #{entry.text}"}
entry.text = "hello"
entry.text += " world"
entry.select_region(0, -1)
vbox.pack_start(entry, true, true, 0)

hbox =, 0)

check ="Editable")
check.signal_connect("toggled") {|w| entry.editable =} = entry.editable?
hbox.pack_start(check, true, true, 0)

check ="Visible")
check.signal_connect("toggled") {|w| entry.visibility =} = entry.visibility?
hbox.pack_start(check, true, true, 0)

button ="Close")
button.signal_connect("clicked") {Gtk.main_quit}
vbox.pack_start(button, true, true, 0)
button.can_default = true

Last modified:2009/01/18 13:54:23
References:[tut-gtk2-btt] [tut-gtk] [tut-gtk2-numtxt]