PyGobject(九十)Pango系列——Pango.Layout

Pango.Layout

Pango.Layout结构表示整个文本段落。一般使用Pango.Context来进行初始化,使用Pango.Layout.set_text(text, length)方法来设置文本;或者使用Widget.create_pango_layout(text)方法来创建,并设置该字符串的属性。

设置完成后,格式化的文本段落就可以从对象中获取,布局将会渲染呈现出来。

Methods

方法修饰词方法名及参数
staticnew (context)
context_changed ()
copy ()
get_alignment ()
get_attributes ()
get_auto_dir ()
get_baseline ()
get_character_count ()
get_context ()
get_cursor_pos (index_)
get_ellipsize ()
get_extents ()
get_font_description ()
get_height ()
get_indent ()
get_iter ()
get_justify ()
get_line (line)
get_line_count ()
get_line_readonly (line)
get_lines ()
get_lines_readonly ()
get_log_attrs ()
get_log_attrs_readonly ()
get_pixel_extents ()
get_pixel_size ()
get_serial ()
get_single_paragraph_mode ()
get_size ()
get_spacing ()
get_tabs ()
get_text ()
get_unknown_glyphs_count ()
get_width ()
get_wrap ()
index_to_line_x (index_, trailing)
index_to_pos (index_)
is_ellipsized ()
is_wrapped ()
move_cursor_visually (strong, old_index, old_trailing, direction)
set_alignment (alignment)
set_attributes (attrs)
set_auto_dir (auto_dir)
set_ellipsize (ellipsize)
set_font_description (desc)
set_height (height)
set_indent (indent)
set_justify (justify)
set_markup (markup, length)
set_markup_with_accel (markup, length, accel_marker)
set_single_paragraph_mode (setting)
set_spacing (spacing)
set_tabs (tabs)
set_text (text, length)
set_width (width)
set_wrap (wrap)
xy_to_index (x, y)

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/6
# section 140
TITLE = "Layout"
DESCRIPTION = """
Pango layout is an object representing a paragraph of text with attributes.
"""
import gi

gi.require_version("Gtk", "3.0")
gi.require_version('PangoCairo', '1.0')
from gi.repository import Gtk, Gdk, Pango, PangoCairo
import cairo

lyrics = """Meet you downstairs in the bar and heard
your rolled up sleeves and your skull t-shirt
You say why did you do it with him today?
and sniff me out like I was Tanqueray

cause you're my fella, my guy
hand me your stella and fly
by the time I'm out the door
you tear men down like Roger Moore

I cheated myself
like I knew I would
I told ya, I was trouble
you know that I'm no good"""


class Area(Gtk.DrawingArea):
    def __init__(self):
        super(Area, self).__init__()
        self.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(16400, 16400, 16440))
        self.connect("draw", self.draw)

    def draw(self, da, cr):
        font_desc = Pango.FontDescription('Sans 15')
        layout = da.create_pango_layout(lyrics)

        layout.set_markup("<span foreground=\"#FF00FF\">%s</span>" % layout.get_text(), -1)
        layout.set_width(Pango.SCALE * self.get_allocation().width)
        layout.set_spacing(Pango.SCALE * 3)
        layout.set_alignment(Pango.Alignment.CENTER)
        layout.set_font_description(font_desc)
        # PangoCairo.layout_path(cr, layout)
        # cr.set_source_rgb(0.5, 0, 0)
        # cr.fill()
        PangoCairo.show_layout(cr, layout)
        return True


class PyApp(Gtk.Window):
    def __init__(self):
        super(PyApp, self).__init__()

        self.connect("destroy", Gtk.main_quit)
        self.set_title("You know I'm no Good")

        self.add(Area())
        self.set_size_request(300, 300)
        self.move((self.get_screen().width() - 300) / 2, (self.get_screen().height() - 300) / 2)
        self.show_all()


def main():
    PyApp()
    Gtk.main()


if __name__ == '__main__':
    main()





代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值