例子
代码:
#!/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()