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

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

TextView

Widget for display of rich text documents.
The TextView widget is used to display text documents. Unlike the Labels, it is intended for long text and supports editing.
Expand/Shrink
  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, 300)
 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       # the textview
 22       self.textview = gtk.TextView() 
 23       textbuffer = self.textview.get_buffer() 
 24       textbuffer.set_text( "You can write here") 
 25       self.mainbox.pack_start( self.textview)
 26       self.textview.show()
 27       # button
 28       b = gtk.Button( "Check text")
 29       self.mainbox.pack_start( b, expand=False)
 30       b.connect( "clicked", self.check_text)
 31       b.show()
 32       # show the box
 33       self.mainbox.show()
 34   
 35     def main( self):
 36       gtk.main()
 37   
 38     def destroy( self, w):
 39       gtk.main_quit()
 40   
 41     def check_text( self, w, data=None):
 42       textbuffer = self.textview.get_buffer() 
 43       startiter, enditer = textbuffer.get_bounds() 
 44       text = textbuffer.get_text( startiter, enditer) 
 45       print text
 46   
 47   if __name__ == "__main__":
 48     m = MyGUI( "TextView example")
 49     m.main()
stdout:
You can write here
whatever you want

AAAA
Screenshot:
Program screenshot gtk-textview1-1.png
Doba běhu: 23230.3 ms
Expand/Shrink
  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, 300)
 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       # the textview
 22       self.textview = gtk.TextView()
 23       textbuffer = self.textview.get_buffer()
 24       self.mainbox.pack_start( self.textview)
 25       self.textview.show()
 26       textbuffer.connect( "changed", self.text_changed) 
 27       # checkboxes
 28       editable = gtk.CheckButton( "Editable")
 29       self.mainbox.pack_start( editable, expand=False)
 30       editable.show()
 31       editable.connect( "toggled", self.editable_toggled) 
 32       editable.set_active( True)
 33       # label for counts
 34       self.count_label = gtk.Label()
 35       self.count_label.show()
 36       self.mainbox.pack_start( self.count_label, expand=False)
 37       self.count_label.set_alignment( 0, 0)
 38       # we set text here because we want the signal to be triggered
 39       # and need the count_label for display of result
 40       textbuffer.set_text( "You can write here")
 41       # button
 42       b = gtk.Button( "Check text")
 43       self.mainbox.pack_start( b, expand=False)
 44       b.connect( "clicked", self.check_text)
 45       b.show()
 46       # show the box
 47       self.mainbox.show()
 48   
 49     def main( self):
 50       gtk.main()
 51   
 52     def destroy( self, w):
 53       gtk.main_quit()
 54   
 55     def check_text( self, w, data=None):
 56       textbuffer = self.textview.get_buffer()
 57       startiter, enditer = textbuffer.get_bounds()
 58       text = textbuffer.get_text( startiter, enditer)
 59       print text
 60   
 61     def editable_toggled( self, w, data=None):
 62       self.textview.set_editable( w.get_active()) 
 63   
 64     def text_changed( self, w, data=None):
 65       chars = w.get_char_count() 
 66       lines = w.get_line_count() 
 67       self.count_label.set_markup( "Chars: <b>%d</b>, Lines: <b>%d</b>" % (chars, lines))
 68   
 69   if __name__ == "__main__":
 70     m = MyGUI( "TextView example")
 71     m.main()
Screenshot:
Program screenshot gtk-textview1-2.pngProgram screenshot gtk-textview1-2a.png
Doba běhu: 1925.6 ms