📄 textview.py
字号:
"subscript", "subscript") text_buffer.insert(iter, " are all supported.\n\n", -1) text_buffer.insert_with_tags_by_name(iter, "Images. ", "heading") text_buffer.insert(iter, "The buffer can have images in it: ", -1) text_buffer.insert_pixbuf(iter, pixbuf) text_buffer.insert_pixbuf(iter, pixbuf) text_buffer.insert_pixbuf(iter, pixbuf) text_buffer.insert(iter, " for example.\n\n", -1) text_buffer.insert_with_tags_by_name(iter, "Spacing. ", "heading") text_buffer.insert(iter, "You can adjust the amount of space before each line.\n", -1) text_buffer.insert_with_tags_by_name(iter, "This line has a whole lot of space before it.\n", "big_gap_before_line", "wide_margins") text_buffer.insert_with_tags_by_name(iter, "You can also adjust the amount of space after each line; " "this line has a whole lot of space after it.\n", "big_gap_after_line", "wide_margins") text_buffer.insert_with_tags_by_name(iter, "You can also adjust the amount of space between wrapped " "lines; this line has extra space between each wrapped line " "in the same paragraph. To show off wrapping, some filler " "text: the quick brown fox jumped over the lazy dog. Blah " "blah blah blah blah blah blah blah blah.\n", "double_spaced_line", "wide_margins") text_buffer.insert(iter, "Also note that those lines have " "extra-wide margins.\n\n", -1) text_buffer.insert_with_tags_by_name(iter, "Editability. ", "heading") text_buffer.insert_with_tags_by_name(iter, "This line is 'locked down' and can't be edited by the " "user - just try it! You can't delete this line.\n\n", "not_editable") text_buffer.insert_with_tags_by_name(iter, "Wrapping. ", "heading") text_buffer.insert(iter, "This line(and most of the others in this buffer) is " "word-wrapped, using the proper Unicode algorithm. Word " "wrap should work in all scripts and languages that GTK+ " "supports. Let's make this a long paragraph to demonstrate: " "blah blah blah blah blah blah blah blah blah blah blah " "blah blah blah blah blah blah blah blah\n\n", -1); text_buffer.insert_with_tags_by_name(iter, "This line has character-based wrapping, and can wrap " "between any two character glyphs. Let's make this a long " "paragraph to demonstrate: blah blah blah blah blah blah " "blah blah blah blah blah blah blah blah blah blah blah " "blah blah\n\n", "char_wrap") text_buffer.insert_with_tags_by_name(iter, "This line has all wrapping turned off, so it makes the " "horizontal scrollbar appear.\n\n\n", "no_wrap") text_buffer.insert_with_tags_by_name(iter, "Justification. ", "heading"); text_buffer.insert_with_tags_by_name(iter, "\nThis line has center justification.\n", "center") text_buffer.insert_with_tags_by_name(iter, "This line has right justification.\n", "right_justify") text_buffer.insert_with_tags_by_name(iter, "\nThis line has big wide margins. Text text text text " "text text text text text text text text text text text " "text text text text text text text text text text text " "text text text text text text text text text text.\n", "wide_margins"); text_buffer.insert_with_tags_by_name(iter, "Internationalization. ", "heading") text_buffer.insert(iter, "You can put all sorts of Unicode text in the buffer.\n\n" "German(Deutsch S\303\274d) Gr\303\274\303\237 Gott\nGreek" "(\316\225\316\273\316\273\316\267\316\275\316\271\316\272" "\316\254) \316\223\316\265\316\271\316\254 \317\203\316\261" "\317\202\nHebrew \327\251\327\234\327\225\327\235\n" "Japanese(\346\227\245\346\234\254\350\252\236)\n\nThe " "widget properly handles bidirectional text, word wrapping, " "DOS/UNIX/Unicode paragraph separators, grapheme boundaries, " "and so on using the Pango internationalization framework.\n", -1) text_buffer.insert(iter, "Here's a word-wrapped quote in a " "right-to-left language:\n", -1) text_buffer.insert_with_tags_by_name(iter, "\331\210\331\202\330\257 \330\250\330\257\330\243 " "\330\253\331\204\330\247\330\253 \331\205\331\206 " "\330\243\331\203\330\253\330\261 \330\247\331\204\331" "\205\330\244\330\263\330\263\330\247\330\252 \330\252" "\331\202\330\257\331\205\330\247 \331\201\331\212 \330" "\264\330\250\331\203\330\251 \330\247\331\203\330\263" "\331\212\331\210\331\206 \330\250\330\261\330\247\331" "\205\330\254\331\207\330\247 \331\203\331\205\331\206" "\330\270\331\205\330\247\330\252 \331\204\330\247 \330" "\252\330\263\330\271\331\211 \331\204\331\204\330\261" "\330\250\330\255\330\214 \330\253\331\205 \330\252\330" "\255\331\210\331\204\330\252 \331\201\331\212 \330\247" "\331\204\330\263\331\206\331\210\330\247\330\252 \330" "\247\331\204\330\256\331\205\330\263 \330\247\331\204" "\331\205\330\247\330\266\331\212\330\251 \330\245\331" "\204\331\211 \331\205\330\244\330\263\330\263\330\247" "\330\252 \331\205\330\247\331\204\331\212\330\251 \331" "\205\331\206\330\270\331\205\330\251\330\214 \331\210" "\330\250\330\247\330\252\330\252 \330\254\330\262\330\241" "\330\247 \331\205\331\206 \330\247\331\204\331\206\330\270" "\330\247\331\205 \330\247\331\204\331\205\330\247\331\204" "\331\212 \331\201\331\212 \330\250\331\204\330\257\330\247" "\331\206\331\207\330\247\330\214 \331\210\331\204\331\203" "\331\206\331\207\330\247 \330\252\330\252\330\256\330\265" "\330\265 \331\201\331\212 \330\256\330\257\331\205\330\251 " "\331\202\330\267\330\247\330\271 \330\247\331\204\331\205\330" "\264\330\261\331\210\330\271\330\247\330\252 \330\247\331\204" "\330\265\330\272\331\212\330\261\330\251. \331\210\330\243" "\330\255\330\257 \330\243\331\203\330\253\330\261 \331\207" "\330\260\331\207 \330\247\331\204\331\205\330\244\330\263" "\330\263\330\247\330\252 \331\206\330\254\330\247\330\255" "\330\247 \331\207\331\210 \302\273\330\250\330\247\331\206" "\331\203\331\210\330\263\331\210\331\204\302\253 \331\201" "\331\212 \330\250\331\210\331\204\331\212\331\201\331\212" "\330\247.\n\n", "rtl_quote") text_buffer.insert(iter, "You can put widgets in the buffer: " "Here's a button: ", -1) anchor = text_buffer.create_child_anchor(iter) text_buffer.insert(iter, " and a menu: ", -1) anchor = text_buffer.create_child_anchor(iter) text_buffer.insert(iter, " and a scale: ", -1) anchor = text_buffer.create_child_anchor(iter) text_buffer.insert(iter, " and an animation: ", -1) anchor = text_buffer.create_child_anchor(iter) text_buffer.insert(iter, " finally a text entry: ", -1) anchor = text_buffer.create_child_anchor(iter) text_buffer.insert(iter, ".\n", -1) text_buffer.insert(iter, "\n\nThis demo doesn't demonstrate all " "the GtkTextBuffer features; it leaves out, for example: " "invisible/hidden text(doesn't work in GTK 2, but planned), " "tab stops, application-drawn areas on the sides of the " "widget for displaying breakpoints and such...", -1) # Apply word_wrap tag to whole buffer */ start, end = text_buffer.get_bounds() text_buffer.apply_tag_by_name("word_wrap", start, end) def attach_widgets(self, text_view): buffer = text_view.get_buffer() iter = buffer.get_start_iter() i = 0 while self.find_anchor(iter): anchor = iter.get_child_anchor() if i == 0: widget = gtk.Button("Click Me") widget.connect("clicked", self.easter_egg_callback) elif i == 1: widget = gtk.combo_box_new_text() widget.append_text("Option 1") widget.append_text("Option 2") widget.append_text("Option 3") elif i == 2: widget = gtk.HScale() widget.set_range(0, 100) widget.set_size_request(70, -1) elif i == 3: widget = gtk.Image() widget.set_from_file(FLOPPYBUDDY_IMAGE) elif i == 4: widget = gtk.Entry() else: raise ValueError text_view.add_child_at_anchor(widget, anchor) widget.show_all() i += 1 return def find_anchor(self, iter): while iter.forward_char(): if iter.get_child_anchor(): return True return False def easter_egg_callback(self, button): if self.win: self.win.present() return buffer = gtk.TextBuffer() iter = buffer.get_start_iter() buffer.insert(iter, "This buffer is shared by a set of nested text views.\n Nested view:\n") anchor = buffer.create_child_anchor(iter) buffer.insert(iter, "\nDon't do this in real applications, please.\n") view = gtk.TextView(buffer) self.recursive_attach_view(0, view, anchor) self.win = gtk.Window() sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.win.add(sw) sw.add(view) self.win.set_default_size(300, 400) self.win.show_all() return def recursive_attach_view(self, depth, view, anchor): if depth > 4: return child_view = gtk.TextView(view.get_buffer()) # Event box is needed to add a black border around each child view event_box = gtk.EventBox() color = gtk.gdk.color_parse("black") event_box.modify_bg(gtk.STATE_NORMAL, color) align = gtk.Alignment(0.5, 0.5, 1.0, 1.0) align.set_border_width(1) event_box.add(align) align.add(child_view) view.add_child_at_anchor(event_box, anchor) self.recursive_attach_view(depth + 1, child_view, anchor) returndef main(): TextViewDemo() gtk.main()if __name__ == '__main__': main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -