例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/6
# section 149
TITLE = "Waiting"
DESCRIPTION = """
In this examle, we use transparency effect to create a waiting demo.
We will draw 8 lines that will gradually fade out creating an illusion
that a line is moving. Such effects are often used to inform users,
that a lengthy task is going on behind the scenes.
An example is streaming video over the internet.
"""
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
import cairo
import math
trs = (
(0.0, 0.15, 0.30, 0.5, 0.65, 0.80, 0.9, 1.0),
(1.0, 0.0, 0.15, 0.30, 0.5, 0.65, 0.8, 0.9),
(0.9, 1.0, 0.0, 0.15, 0.3, 0.5, 0.65, 0.8),
(0.8, 0.9, 1.0, 0.0, 0.15, 0.3, 0.5, 0.65),
(0.65, 0.8, 0.9, 1.0, 0.0, 0.15, 0.3, 0.5),
(0.5, 0.65, 0.8, 0.9, 1.0, 0.0, 0.15, 0.3),
(0.3, 0.5, 0.65, 0.8, 0.9, 1.0, 0.0, 0.15),
(0.15, 0.3, 0.5, 0.65, 0.8, 0.9, 1.0, 0.0,)
)
class PyApp(Gtk.Window):
def __init__(self):
super(PyApp, self).__init__()
self.set_title("Waiting")
self.set_size_request(250, 150)
self.connect("destroy", Gtk.main_quit)
self.darea = Gtk.DrawingArea()
self.darea.connect("draw", self.draw)
self.add(self.darea)
self.count = 0
GLib.timeout_add(100, self.on_timer)
self.show_all()
def on_timer(self):
self.count += 1
self.darea.queue_draw()
return True
def draw(self, drawingArea, cr):
w = drawingArea.get_allocated_width()
h = drawingArea.get_allocated_height()
cr.set_line_width(3)
cr.set_line_cap(cairo.LINE_CAP_ROUND)
cr.translate(w / 2, h / 2)
for i in range(8):
cr.set_source_rgba(0, 0, 0, trs[self.count % 8][i])
cr.move_to(0.0, -10.0)
cr.line_to(0.0, -40.0)
cr.rotate(math.pi / 4)
cr.stroke()
def main():
PyApp()
Gtk.main()
if __name__ == "__main__":
main()