PyGobject(九十八)Cairo系列——动态文本

本文介绍了一个使用Python和GTK创建的Puff效果示例。该效果展示了一段居中文字逐渐放大并淡出的过程,常用于Flash动画等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/6
# section 148
TITLE = "Puff"
DESCRIPTION = """
In the following example, we create a puff effect. The example will display a growing
centered text that will gradually fade out from some point.
This is a very common effect, which you can often see in flash animations
"""
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
import cairo


class PyApp(Gtk.Window):
    def __init__(self):
        super(PyApp, self).__init__()
        self.set_title("Puff")
        self.resize(350, 200)
        self.connect("destroy", Gtk.main_quit)
        self.darea = Gtk.DrawingArea()
        self.darea.connect("draw", self.draw)
        self.add(self.darea)
        self.timer = True
        self.alpha = 1.0
        self.size = 1.0
        GLib.timeout_add(50, self.on_timer)
        self.show_all()

    def on_timer(self):
        if not self.timer:
            return False
        self.darea.queue_draw()
        return True

    def draw(self, drawingArea, cr):
        w = drawingArea.get_allocated_width()
        h = drawingArea.get_allocated_height()
        cr.set_source_rgb(0.5, 0, 0)
        cr.paint()
        cr.select_font_face("Courier", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
        self.size += 0.8
        if self.size > 20:
            self.alpha -= 0.01
        cr.set_font_size(self.size)
        cr.set_source_rgb(1, 1, 1)
        (x, y, width, height, dx, dy) = cr.text_extents("ZetCode")
        cr.move_to(w / 2 - width / 2, h / 2)
        cr.text_path("ZetCode")
        cr.clip()
        cr.stroke()
        cr.paint_with_alpha(self.alpha)
        if self.alpha <= 0:
            self.size = 1.0
            self.alpha = 1.0


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


if __name__ == "__main__":
    main()





代码下载地址:http://download.youkuaiyun.com/detail/a87b01c14/9594728

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sanxiaochengyu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值