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

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

TextView with rich text

How to display rich text in TextView.
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   import pango 
  7   
  8   class MyGUI:
  9   
 10     def __init__( self, title):
 11       self.window = gtk.Window()
 12       self.title = title
 13       self.window.set_title( title)
 14       self.window.set_size_request( -1, 300)
 15       self.window.connect( "destroy", self.destroy)
 16       self.create_interior()
 17       self.window.show_all()
 18   
 19     def create_interior( self):
 20       self.mainbox = gtk.VBox()
 21       self.window.add( self.mainbox)
 22       # the textview
 23       self.textview = gtk.TextView()
 24       self.textbuffer = self.textview.get_buffer()
 25       self.mainbox.pack_start( self.textview)
 26       self.textview.show()
 27       # fill the text buffer
 28       # heading
 29       h_tag = self.textbuffer.create_tag( "h", size_points=16, weight=pango.WEIGHT_BOLD) 
 30       position = self.textbuffer.get_end_iter()
 31       self.textbuffer.insert_with_tags( position, "Heading\n", h_tag) 
 32       # normal text
 33       position = self.textbuffer.get_end_iter()
 34       self.textbuffer.insert( position, "Several lines\nof normal text.\n") 
 35       # more text
 36       position = self.textbuffer.get_end_iter()
 37       i_tag = self.textbuffer.create_tag( "i", style=pango.STYLE_ITALIC) 
 38       self.textbuffer.insert_with_tags( position, "italic text", i_tag) 
 39       position = self.textbuffer.get_end_iter()
 40       self.textbuffer.insert_with_tags( position, " combined with heading", i_tag, h_tag) 
 41       position = self.textbuffer.get_end_iter()
 42       c_tag = self.textbuffer.create_tag( "colored", foreground="#FFFF00", background="#0000FF") 
 43       self.textbuffer.insert_with_tags( position, "\nand color", i_tag, h_tag, c_tag) 
 44       e_tag = self.textbuffer.create_tag( "fixed", editable=False) 
 45       position = self.textbuffer.get_end_iter()
 46       self.textbuffer.insert_with_tags( position, "\nnon-editable ", e_tag) 
 47       position = self.textbuffer.get_end_iter()    
 48       self.textbuffer.insert_with_tags_by_name( position, " and colored text", "colored", "fixed") 
 49       # show the box
 50       self.mainbox.show()
 51   
 52     def main( self):
 53       gtk.main()
 54   
 55     def destroy( self, w):
 56       gtk.main_quit()
 57   
 58   if __name__ == "__main__":
 59     m = MyGUI( "TextView example II.")
 60     m.main()
Screenshot:
Program screenshot gtk-textview2-1.png
Doba běhu: 2409.0 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   import pango 
  7   
  8   class MyGUI:
  9   
 10     def __init__( self, title):
 11       self.window = gtk.Window()
 12       self.title = title
 13       self.window.set_title( title)
 14       self.window.set_size_request( 400, 300)
 15       self.window.connect( "destroy", self.destroy)
 16       self.create_interior()
 17       self.window.show_all()
 18   
 19     def create_interior( self):
 20       self.mainbox = gtk.VBox()
 21       self.window.add( self.mainbox)
 22       # the textview
 23       self.textview = gtk.TextView()
 24       self.textbuffer = self.textview.get_buffer()
 25       self.mainbox.pack_start( self.textview)
 26       self.textview.show()
 27       # fill the text buffer
 28       # heading
 29       b_tag = self.textbuffer.create_tag( "b", weight=pango.WEIGHT_BOLD) 
 30       i_tag = self.textbuffer.create_tag( "i", style=pango.STYLE_ITALIC) 
 31       u_tag = self.textbuffer.create_tag( "u", underline=pango.UNDERLINE_SINGLE) 
 32       self.styles = [] # here we will collect active style names 
 33       # connect the event *after* all internal handlers were run, 
 34       # otherwise it will run before any text is actually inserted 
 35       self.textbuffer.connect_after( "insert-text", self.text_inserted) 
 36       styles = {"Bold":"b", "Italic": "i", "Underline": "u"}
 37       for name, style in styles.iteritems():
 38           check = gtk.CheckButton( name)
 39           check.connect( "toggled", self.style_changed, style)
 40           check.show()
 41           self.mainbox.pack_start( check, expand=False)
 42       # apply style button
 43       b = gtk.Button( "Apply style to selection")
 44       b.connect( "clicked", self.apply_style)
 45       b.show()
 46       self.mainbox.pack_start( b, expand=False)
 47       # show the box
 48       self.mainbox.show()
 49   
 50     def text_inserted( self, w, end, text, length):
 51       start = self.textbuffer.get_iter_at_offset( end.get_offset() - length) 
 52       self.textbuffer.remove_all_tags( start, end) 
 53       for style in self.styles:
 54         self.textbuffer.apply_tag_by_name( style, start, end) 
 55   
 56     def apply_style( self, w):
 57       if self.textbuffer.get_has_selection():
 58         start, end = self.textbuffer.get_selection_bounds() 
 59         self.textbuffer.remove_all_tags( start, end) 
 60         for style in self.styles:
 61           self.textbuffer.apply_tag_by_name( style, start, end) 
 62   
 63     def style_changed( self, w, style):
 64       if w.get_active():
 65         self.styles.append( style)
 66       else:
 67         self.styles.remove( style)
 68       self.textview.grab_focus() # return focus to the window 
 69         
 70     def main( self):
 71       gtk.main()
 72   
 73     def destroy( self, w):
 74       gtk.main_quit()
 75   
 76   if __name__ == "__main__":
 77     m = MyGUI( "TextView example II.")
 78     m.main()
Screenshot:
Program screenshot gtk-textview2-2.png
Doba běhu: 1661.5 ms