Python-Ref > GUI programming with PyGTK > Widgets > Entry
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Entry

Simple widget for single-line text input from the user.
The Entry widget provides a single line text entry. The user can input text into it and also use some common keyboard shortcuts there, such as "Home" and "End" to move the cursor inside the entry.
Expand/Shrink
Zdroj: (gtk-entry1-1.py)
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   
  7   class MyGUI:
  8   
  9     def __init__( self, title):
 10       self.window = gtk.Window()
 11       self.title = title
 12       self.window.set_title( title)
 13       self.window.set_size_request( 260, -1)
 14       self.window.connect( "destroy", self.destroy)
 15       self.create_interior()
 16       self.window.show_all()
 17   
 18     def create_interior( self):
 19       self.mainbox = gtk.VBox()
 20       self.window.add( self.mainbox)
 21       # label for answer
 22       self.answer_label = gtk.Label( "")
 23       self.mainbox.pack_start( self.answer_label, padding=10)
 24       self.answer_label.show()
 25       # box for label and entry
 26       hbox = gtk.HBox()
 27       self.mainbox.pack_start( hbox, expand=False)
 28       label = gtk.Label( "Give me your name:")
 29       hbox.pack_start( label, expand=False)
 30       self.entry = gtk.Entry( max=20) 
 31       hbox.pack_start( self.entry)
 32       self.entry.show()
 33       # button
 34       b = gtk.Button( "Say hello")
 35       self.mainbox.pack_start( b, expand=False)
 36       b.connect( "clicked", self.say_hello)
 37       b.show()
 38       # show the box
 39       self.mainbox.show()
 40   
 41     def main( self):
 42       gtk.main()
 43   
 44     def destroy( self, w):
 45       gtk.main_quit()
 46   
 47     def say_hello( self, w, data=None):
 48       name = self.entry.get_text() 
 49       self.answer_label.set_label( "Hi, %s." % name)
 50   
 51   
 52   if __name__ == "__main__":
 53     m = MyGUI( "Entry example")
 54     m.main()
Screenshot:
Program screenshot gtk-entry1-1.pngProgram screenshot gtk-entry1-1a.png
Doba běhu: 2484.0 ms
Expand/Shrink
Rozdíl proti: gtk-entry1-1.py
@@ -28,6 +28,7 @@
     label = gtk.Label( "Give me your name:")
     hbox.pack_start( label, expand=False)
     self.entry = gtk.Entry( max=20) 
+    self.entry.connect( "changed", self.say_hello) 
     hbox.pack_start( self.entry)
     self.entry.show()
     # button
@@ -45,8 +46,11 @@
     gtk.main_quit()
   def say_hello( self, w, data=None):
-    name = self.entry.get_text() 
-    self.answer_label.set_label( "Hi, %s." % name)
+    name = self.entry.get_text()
+    if name.strip():
+      self.answer_label.set_label( "Hi, %s." % name)
+    else:
+      self.answer_label.set_label( "")
 if __name__ == "__main__":
Screenshot:
Program screenshot gtk-entry1-2.pngProgram screenshot gtk-entry1-2a.png
Doba běhu: 3490.6 ms
Expand/Shrink
Rozdíl proti: gtk-entry1-2.py
@@ -29,6 +29,8 @@
     hbox.pack_start( label, expand=False)
     self.entry = gtk.Entry( max=20) 
     self.entry.connect( "changed", self.say_hello) 
+    # the following triggers the "changed" event 
+    self.entry.set_text( "Guest") 
     hbox.pack_start( self.entry)
     self.entry.show()
     # button
Screenshot:
Program screenshot gtk-entry1-3.png
Doba běhu: 1086.3 ms