⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textview.py

📁 使用Pygtk写的几个例子
💻 PY
📖 第 1 页 / 共 2 页
字号:
#!/usr/bin/env python"""Text Widget/TextViewThe GtkTextView widget displays a GtkTextBuffer. One GtkTextBuffer can be displayedby multiple GtkTextViews. This demo has two views displaying a single buffer, andshows off the widget's text formatting features."""# pygtk version: Maik Hertha <maik.hertha@berlin.de>import osimport sysimport gobjectimport gtkgray50_width  = 2gray50_height = 2gray50_bits   = '\x02\x01'GTKLOGO_IMAGE = os.path.join(os.path.dirname(__file__),                             'images', 'gtk-logo-rgb.gif')FLOPPYBUDDY_IMAGE = os.path.join(os.path.dirname(__file__),                                 'images', 'floppybuddy.gif')class TextViewDemo(gtk.Window):    def __init__(self, parent=None):        # Create the toplevel window        gtk.Window.__init__(self)        try:            self.set_screen(parent.get_screen())        except AttributeError:            self.connect('destroy', lambda *w: gtk.main_quit())        self.set_title(self.__class__.__name__)        self.set_default_size(450, 450)        self.set_border_width(0)        vpaned = gtk.VPaned()        vpaned.set_border_width(5)        self.add(vpaned)        # For convenience, we just use the autocreated buffer from        # the first text view; you could also create the buffer        # by itself with gtk.text_buffer_new(), then later create        # a view widget.        view1 = gtk.TextView();        buffer_1 = view1.get_buffer()        view2 = gtk.TextView(buffer_1)        sw = gtk.ScrolledWindow()        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)        vpaned.add1(sw)        sw.add(view1)        sw = gtk.ScrolledWindow()        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)        vpaned.add2(sw)        sw.add(view2)        self.create_tags(buffer_1)        self.insert_text(buffer_1)        self.attach_widgets(view1)        self.attach_widgets(view2)        self.win = None        self.show_all()    def create_tags(self, text_buffer):        '''        Create a bunch of tags. Note that it's also possible to        create tags with gtk.text_tag_new() then add them to the        tag table for the buffer, text_buffer.create_tag() is        just a convenience function. Also note that you don't have        to give tags a name; pass None for the name to create an        anonymous tag.        In any real app, another useful optimization would be to create        a GtkTextTagTable in advance, and reuse the same tag table for        all the buffers with the same tag set, instead of creating        new copies of the same tags for every buffer.        Tags are assigned default priorities in order of addition to the        tag table. That is, tags created later that affect the same text        property affected by an earlier tag will override the earlier        tag. You can modify tag priorities with        gtk.text_tag_set_priority().        '''        import pango        text_buffer.create_tag("heading",                    weight=pango.WEIGHT_BOLD,                    size=15 * pango.SCALE)        text_buffer.create_tag("italic", style=pango.STYLE_ITALIC)        text_buffer.create_tag("bold", weight=pango.WEIGHT_BOLD)                                # points times the pango.SCALE factor        text_buffer.create_tag("big", size=20 * pango.SCALE)        text_buffer.create_tag("xx-small", scale=pango.SCALE_XX_SMALL)        text_buffer.create_tag("x-large", scale=pango.SCALE_X_LARGE)        text_buffer.create_tag("monospace", family="monospace")        text_buffer.create_tag("blue_foreground", foreground="blue")        text_buffer.create_tag("red_background", background="red")        stipple = gtk.gdk.bitmap_create_from_data(None,            gray50_bits, gray50_width, gray50_height)        text_buffer.create_tag("background_stipple", background_stipple=stipple)        text_buffer.create_tag("foreground_stipple", foreground_stipple=stipple)        text_buffer.create_tag("big_gap_before_line", pixels_above_lines=30)        text_buffer.create_tag("big_gap_after_line", pixels_below_lines=30)        text_buffer.create_tag("double_spaced_line", pixels_inside_wrap=10)        text_buffer.create_tag("not_editable", editable=False)        text_buffer.create_tag("word_wrap", wrap_mode=gtk.WRAP_WORD)        text_buffer.create_tag("char_wrap", wrap_mode=gtk.WRAP_CHAR)        text_buffer.create_tag("no_wrap", wrap_mode=gtk.WRAP_NONE)        text_buffer.create_tag("center", justification=gtk.JUSTIFY_CENTER)        text_buffer.create_tag("right_justify", justification=gtk.JUSTIFY_RIGHT)        text_buffer.create_tag("wide_margins",                    left_margin=50, right_margin=50)        text_buffer.create_tag("strikethrough", strikethrough=True)        text_buffer.create_tag("underline",                    underline=pango.UNDERLINE_SINGLE)        text_buffer.create_tag("double_underline",                    underline=pango.UNDERLINE_DOUBLE)        text_buffer.create_tag("superscript",                    rise=10 * pango.SCALE,      # 10 pixels                    size=8 * pango.SCALE)       #  8 points        text_buffer.create_tag("subscript",                    rise=-10 * pango.SCALE,     # 10 pixels                    size=8 * pango.SCALE)       #  8 points        text_buffer.create_tag("rtl_quote",                    wrap_mode=gtk.WRAP_WORD, direction=gtk.TEXT_DIR_RTL,                    indent=30, left_margin=20, right_margin=20)    def insert_text(self, text_buffer):        # use the current directory for the file        try:            pixbuf = gtk.gdk.pixbuf_new_from_file(GTKLOGO_IMAGE)        except gobject.GError, error:            sys.exit("Failed to load image file gtk-logo-rgb.gif\n")        scaled = pixbuf.scale_simple(32, 32, 'bilinear')        pixbuf = scaled        # get start of buffer; each insertion will revalidate the        # iterator to point to just after the inserted text.        iter = text_buffer.get_iter_at_offset(0)        text_buffer.insert(iter, "The text widget can display text with "            "all kinds of nifty attributes. It also supports multiple views "            "of the same buffer; this demo is showing the same buffer in "            "two places.\n\n")        text_buffer.insert_with_tags_by_name(iter, "Font styles. ", "heading")        text_buffer.insert(iter, "For example, you can have ")        text_buffer.insert_with_tags_by_name(iter,                            "italic", "italic")        text_buffer.insert(iter, ", ");        text_buffer.insert_with_tags_by_name(iter,                            "bold", "bold")        text_buffer.insert(iter, ", or ", -1)        text_buffer.insert_with_tags_by_name(iter,                            "monospace(typewriter)", "monospace")        text_buffer.insert(iter, ", or ")        text_buffer.insert_with_tags_by_name(iter,                            "big", "big")        text_buffer.insert(iter, " text. ")        text_buffer.insert(iter, "It's best not to hardcode specific text "            "sizes; you can use relative sizes as with CSS, such as ")        text_buffer.insert_with_tags_by_name(iter,                            "xx-small", "xx-small")        text_buffer.insert(iter, " or ")        text_buffer.insert_with_tags_by_name(iter,                            "x-large", "x-large")        text_buffer.insert(iter, " to ensure that your program properly "            "adapts if the user changes the default font size.\n\n")        text_buffer.insert_with_tags_by_name(iter, "Colors. ", "heading")        text_buffer.insert(iter, "Colors such as ");        text_buffer.insert_with_tags_by_name(iter,                            "a blue foreground", "blue_foreground")        text_buffer.insert(iter, " or ");        text_buffer.insert_with_tags_by_name(iter,                            "a red background",                            "red_background")        text_buffer.insert(iter, " or even ", -1);        text_buffer.insert_with_tags_by_name(iter,                            "a stippled red background",                            "red_background",                            "background_stipple")        text_buffer.insert(iter, " or ", -1);        text_buffer.insert_with_tags_by_name(iter,                            "a stippled blue foreground on solid red background",                            "blue_foreground",                            "red_background",                            "foreground_stipple")        text_buffer.insert(iter, "(select that to read it) can be used.\n\n", -1);        text_buffer.insert_with_tags_by_name(iter,            "Underline, strikethrough, and rise. ", "heading")        text_buffer.insert_with_tags_by_name(iter,                            "Strikethrough",                            "strikethrough")        text_buffer.insert(iter, ", ", -1)        text_buffer.insert_with_tags_by_name(iter,                            "underline",                            "underline")        text_buffer.insert(iter, ", ", -1)        text_buffer.insert_with_tags_by_name(iter,                            "double underline",                            "double_underline")        text_buffer.insert(iter, ", ", -1)        text_buffer.insert_with_tags_by_name(iter,                            "superscript",                            "superscript")        text_buffer.insert(iter, ", and ", -1)        text_buffer.insert_with_tags_by_name(iter,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -