| PyGTK Tutorial | ||
|---|---|---|
| <<< Previous | Chapter 9. Miscellaneous Widgets | Next >>> |
There are two functions for creating Entry
widgets:
entry = GtkEntry() entry = GtkEntry(max) |
The first just creates a new Entry widget, whilst the second creates a new Entry and sets a limit of max characters on the length of the text within the Entry.
There are several methods for altering
the text which is currently within the Entry widget.
entry.set_text(text) entry.append_text(text) entry.prepend_text(text) |
The set_text() method sets the contents of the Entry widget to text, replacing the current contents. The append_text() and prepend_text() allow the current contents to be appended or prepended with text.
The next method allows the current insertion
point to be set.
entry.set_position(position) |
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(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 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.9 shows the result of running the program:

1 #!/usr/bin/env python
2
3 # example entry.py
4
5 import gtk
6
7 class EntryExample:
8 def enter_callback(self, widget, entry):
9 entry_text = entry.get_text()
10 print "Entry contents: %s\n" % entry_text
11
12 def entry_toggle_editable(self, checkbutton, entry):
13 entry.set_editable(checkbutton.active)
14
15 def entry_toggle_visibility(self, checkbutton, entry):
16 entry.set_visibility(checkbutton.active)
17
18 def __init__(self):
19 # create a new window
20 window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
21 window.set_usize(200, 100)
22 window.set_title("GTK Entry")
23 window.connect("delete_event", gtk.mainquit)
24
25 vbox = gtk.GtkVBox(gtk.FALSE, 0)
26 window.add(vbox)
27 vbox.show()
28
29 entry = gtk.GtkEntry(50)
30 entry.connect("activate", self.enter_callback, entry)
31 entry.set_text("hello")
32 entry.append_text(" world")
33 entry.select_region(0, len(entry.get_text()))
34 vbox.pack_start(entry, gtk.TRUE, gtk.TRUE, 0)
35 entry.show()
36
37 hbox = gtk.GtkHBox(gtk.FALSE, 0)
38 vbox.add(hbox)
39 hbox.show()
40
41 check = gtk.GtkCheckButton("Editable")
42 hbox.pack_start(check, gtk.TRUE, gtk.TRUE, 0)
43 check.connect("toggled", self.entry_toggle_editable, entry)
44 check.set_active(gtk.TRUE)
45 check.show()
46
47 check = gtk.GtkCheckButton("Visible")
48 hbox.pack_start(check, gtk.TRUE, gtk.TRUE, 0)
49 check.connect("toggled", self.entry_toggle_visibility, entry)
50 check.set_active(gtk.TRUE)
51 check.show()
52
53 button = gtk.GtkButton("Close")
54 button.connect_object("clicked", gtk.mainquit, window)
55 vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 0)
56 button.set_flags(gtk.CAN_DEFAULT)
57 button.grab_default()
58 button.show()
59 window.show()
60
61 def main():
62 gtk.mainloop()
63 return 0
64
65 if __name__ == "__main__":
66 EntryExample()
67 main()
|
| <<< Previous | Home | Next >>> |
| Statusbars | Up | Spin Buttons |