#!/usr/bin/env python
"""Text Widget/TextView
The GtkTextView widget displays a GtkTextBuffer. One GtkTextBuffer can be displayed
by multiple GtkTextViews. This demo has two views displaying a single buffer, and
shows off the widget's text formatting features."""
# pygtk version: Maik Hertha <maik.hertha@berlin.de>
import os
import sys
import gobject
import gtk
gray50_width = 2
gray50_height = 2
gray50_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 "
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
PyGTK+-demo.rar (25个子文件)
PyGTK+-demo
Images.py 10KB
Entry-Completion.py 2KB
Text-Widgets-TextView.py 20KB
Status-Icon.py 781B
Tree-View-Editable-Cells.py 5KB
Text-Widgets-Automatic-scrolling.py 4KB
Change-Display.py 14KB
Tooltip.py 9KB
Tree-View-Generic-Tree-Model.py 4KB
Color-Selector.py 2KB
Expander.py 1KB
Text-Widgets-Hypertext.py 7KB
Dialog-and-Message-Boxes.py 4KB
Menu.py 2KB
Stock-Item-and-Icon-Browser.py 8KB
Print-Editor.py 14KB
Pixbufs.py 6KB
Application-main-window.py 11KB
Button-Box.py 2KB
Tree-View-List-Store.py 5KB
Size-Group.py 4KB
Tree-View-Tree-Store.py 11KB
UI-Manager.py 6KB
Paned-Widgets.py 4KB
Drag-and-Drop.py 7KB
共 25 条
- 1
资源评论
rtoax
- 粉丝: 2753
- 资源: 218
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功