Entry boxes support the input of text or numerical values via the keyboard or similar input devices. They usually support the most common editing operations such as character input, deletion, etc.
The Editable
class is an abstract base
class, which takes care of dealing with keyboard events and text
caret positioning. It is used as backend for the
Entry
widget class, which will be
explained in the next section.
You usually will not create an Editable
object directly, but rather use it as parent for your own
classes.
It can store and operate on (unicode) text through its
text attribute. The text usually will be
set via this attribute or the
set_text()
method.
editable.text = "Text" editable.set_text ("Text")
It also supports a virtual text caret position, which will be
used by its internals to determine the position for editing
operations. The caret attribute and
set_caret()
allow you to adjust the
caret position.
editable.caret = 7 editable.set_caret (7)
Given both the text and
caret attribute, the internals of the
Editable
will modify the text of it upon
receiving keyboard events, if it has the input focus. Given an
Editable
, which contains the text
"This is a Test." and its caret is set to
4, the processing of a pressed key ("D") causes:
Check, whether text editing is allowed.
If it is, insert a "D" at the fourth position in the text.
The text of it now is "ThisD is a Test"
The previous list mentioned a check, whether the text of an
Editable
can be modified. To allow or
disallow editing the hold text, you can adjust the value of the
editable attribute.
editable.editable = True editable.set_editable (True)
set_text()
method.
The Editable
listens by default to the
following signals:
SIG_KEYDOWN - Invoked, when a key gets pressed.
SIG_INPUT - Invoked, when the input is validated or aborted using RETURN or ESC.
The Entry
widget is a single line text
input box, that inherits from the Editable
class. It fully supports the Editable
features and enhances it by mouse event sensitivity.
To create a Entry
widget, you usually
will type
entry = Entry (text)
Entry
widgets support a password-like
mode, in which any typed character will be displayed as an
asterisk ('*').
entry.password = True
To allow a better look for some special fonts, the
Entry
can place an additional amount of
pixels between its border and the text input area via the
padding attribute or
set_padding()
method.
entry.padding = 5 entry.set_padding (10)
Entry
by default.
The Entry
supports the SIG_MOUSEDOWN
signal to get the input focus upon a left
mouse button press.
Below you will find an example to illustrate most of the
abilities of the Entry
widget class. You
do not need to care about other widgets like the
Frame
class for now as those are
explained later on.
You can find the following example as a python script under
examples/entry.py
.
# Entry examples. from ocempgui.widgets import * from ocempgui.widgets.Constants import * def _toggle_password (button, entry): entry.password = button.active def create_entry_view (): states = ("STATE_NORMAL", "STATE_ENTERED", "STATE_ACTIVE", "STATE_INSENSITIVE") table = Table (2, 3) table.spacing = 5 table.set_row_align (0, ALIGN_TOP) # Frame with the states. frm_states = VFrame (Label ("States")) frm_states.spacing = 5 frm_states.align = ALIGN_LEFT for i in xrange (len (states)): entry = Entry (states[i]) if STATE_TYPES[i] == STATE_INSENSITIVE: entry.sensitive = False else: entry.state = STATE_TYPES[i] frm_states.add_child (entry) table.add_child (0, 0, frm_states) # Frame with different padding. frm_padding = VFrame (Label ("Padding")) frm_padding.spacing = 5 frm_padding.align = ALIGN_LEFT for i in xrange (5): entry = Entry ("Padding: %dpx" % (i * 2)) entry.padding = i * 2 frm_padding.add_child (entry) table.add_child (0, 1, frm_padding) # Password Mode frm_password = VFrame (Label ("Password support")) frm_padding.spacing = 5 entry = Entry ("A password") button = ToggleButton ("Password mode") button.connect_signal (SIG_TOGGLED, _toggle_password, button, entry) frm_password.add_child (entry, button) table.add_child (0, 2, frm_password) return table if __name__ == "__main__": # Initialize the drawing window. re = Renderer () re.create_screen (400, 400) re.title = "Entry examples" re.color = (234, 228, 223) re.add_widget (create_entry_view ()) # Start the main rendering loop. re.start ()
Example 33. Entry example