|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="entry.py" xml:lang="gl">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Entry (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#entry"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="strings.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="scale.py"/>
|
|
Packit |
1470ea |
<revision version="0.2" date="2012-06-23" status="draft"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright editor">
|
|
Packit |
1470ea |
<name>Marta Maria Casetti</name>
|
|
Packit |
1470ea |
<email its:translate="no"/>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Sebastian Pölsterl</name>
|
|
Packit |
1470ea |
<email its:translate="no">sebp@k-d-w.org</email>
|
|
Packit |
1470ea |
<years>2011</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
<desc>A single line text entry field</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Fran Dieguez</mal:name>
|
|
Packit |
1470ea |
<mal:email>frandieguez@gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012-2013.</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Entry</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/entry.png"/>
|
|
Packit |
1470ea |
This application greets you in the terminal with the name you provide.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Código usado para xerar este exemplo</title>
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="What is your name?", application=app)
|
|
Packit |
1470ea |
self.set_default_size(300, 100)
|
|
Packit |
1470ea |
self.set_border_width(10)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a single line entry
|
|
Packit |
1470ea |
name_box = Gtk.Entry()
|
|
Packit |
1470ea |
# emits a signal when the Enter key is pressed, connected to the
|
|
Packit |
1470ea |
# callback function cb_activate
|
|
Packit |
1470ea |
name_box.connect("activate", self.cb_activate)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# add the Gtk.Entry to the window
|
|
Packit |
1470ea |
self.add(name_box)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the content of the entry is used to write in the terminal
|
|
Packit |
1470ea |
def cb_activate(self, entry):
|
|
Packit |
1470ea |
# retrieve the content of the widget
|
|
Packit |
1470ea |
name = entry.get_text()
|
|
Packit |
1470ea |
# print it in a nice form in the terminal
|
|
Packit |
1470ea |
print("Hello " + name + "!")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication(Gtk.Application):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Application.__init__(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_activate(self):
|
|
Packit |
1470ea |
win = MyWindow(self)
|
|
Packit |
1470ea |
win.show_all()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="methods">
|
|
Packit |
1470ea |
<title>Useful methods for an Entry widget</title>
|
|
Packit |
1470ea |
In line 14 the signal "activate" is connected to the callback function cb_activate() using widget.connect(signal, callback function) . See <link xref="signals-callbacks.py"/> for a more detailed explanation. Some of the signals that a Gtk.Entry widget can emit are: "activate" (emitted when the user activates the Entry key); "backspace" (emitted when the user activates the Backspace or Shift-Backspace keys); "copy-clipboard" (Ctrl-c and Ctrl-Insert); "paste-clipboard" (Ctrl-v and Shift-Insert); "delete-from-cursor" (Delete, for deleting a character; Ctrl-Delete, for deleting a word); "icon-press" (emitted when the user clicks an activatable icon); "icon-release" (emitted on the button release from a mouse click over an activatable icon); "insert-at-cursor" (emitted when the user initiates the insertion of a fixed string at the cursor); "move-cursor" (emitted when the user initiates a cursor movement); "populate-popup" (emitted before showing the context menu of the entry; it can be used to add items to it).
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>get_buffer() and set_buffer(buffer) , where buffer is a Gtk.EntryBuffer object, can be used to get and set the buffer for the entry. </item>
|
|
Packit |
1470ea |
<item>get_text() and set_text("some text") can be used to get and set the content for the entry. </item>
|
|
Packit |
1470ea |
<item>get_text_length() is self-explanatory. </item>
|
|
Packit |
1470ea |
<item>get_text_area() gets the area where the entry's text is drawn. </item>
|
|
Packit |
1470ea |
<item>If we set set_visibility(False) the characters in the entry are displayed as the invisible char. This is the best available in the current font, but it can be changed with set_invisible_char(ch) , where ch is a Unicode character. The latter method is reversed by unset_invisible_char() . </item>
|
|
Packit |
1470ea |
<item>set_max_length(int) , where int is an integer, truncates every entry longer than int to have the desired maximum length. </item>
|
|
Packit |
1470ea |
<item>By default, if you press the Entry key the Gtk.Entry emits the signal "activate" . If you would like to activate the default widget for the window (set using set_default(widget) on the window), then use set_activates_default(True) . </item>
|
|
Packit |
1470ea |
<item>To set a frame around the entry: set_has_frame(True) . </item>
|
|
Packit |
1470ea |
<item>set_placeholder_text("some text") sets the text to be displayed in the entry when it is empty and unfocused. </item>
|
|
Packit |
1470ea |
<item>set_overwrite_mode(True) and set_overwrite_mode(False) are self-explanatory. </item>
|
|
Packit |
1470ea |
<item>If we have set_editable(False) the user cannot edit the text in the widget. </item>
|
|
Packit |
1470ea |
<item>set_completion(completion) , where completion is a <link href="http://developer.gnome.org/gtk3/unstable/GtkEntryCompletion.html">Gtk.EntryCompletion </link>, sets completion - or disables it if completion is None . </item>
|
|
Packit |
1470ea |
<item>An Entry widget can display progress or activity information behind the text. We use set_progress_fraction(fraction) , where fraction is a float between 0.0 and 1.0 inclusive, to fill in the given fraction of the bar. We use set_progress_pulse_step() to set the fraction of total entry width to move the progress bouncing block for each call to progress_pulse() . The latter method indicates that some progress is made, and causes the progress indicator of the entry to enter "activity mode", where a block bounces back and forth. Each call to progress_pulse() causes the block to move by a little bit (the amount of movement per pulse is determined, as said before, by set_progress_pulse_step() ). </item>
|
|
Packit |
1470ea |
<item>An Entry widget can also show icons. These icons can be activatable by clicking, can be set up as drag source and can have tooltips. To add an icon, use set_icon_from_stock(icon_position, stock_id) , or one of set_icon_from_pixbuf(icon_position, pixbuf) , set_icon_from_icon_name(icon_position, icon_name) , where icon_position is one of Gtk.EntryIconPosition.PRIMARY (to set the icon at the beginning of the entry) Gtk.EntryIconPosition.SECONDARY (to set the icon at the end of the entry). To set a tooltip on an icon, use set_icon_tooltip_text("tooltip text") or set_icon_tooltip_markup("tooltip text in Pango markup language") . </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>API References</title>
|
|
Packit |
1470ea |
Neste exemplo empregaremos o seguinte:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkEntry.html">GtkEntry</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|