Skip to content

Instantly share code, notes, and snippets.

@infirit
Created August 14, 2015 19:29
Show Gist options
  • Select an option

  • Save infirit/3318546f2e5d8f784f4e to your computer and use it in GitHub Desktop.

Select an option

Save infirit/3318546f2e5d8f784f4e to your computer and use it in GitHub Desktop.
Fade-in pixbuf box
#!/usr/bin/python
from gi.repository import Gtk, GLib, Gdk
import cairo
class FadeBoxWindow(Gtk.Window):
def __init__(self):
super(FadeBoxWindow, self).__init__()
self.init_ui()
def init_ui(self):
self.darea = Gtk.DrawingArea()
self.darea.connect("draw", self.on_draw)
self.darea.connect_after("draw", self.on_draw_after)
self.add(self.darea)
self.timer = True
self.alpha = 0.0
GLib.timeout_add(20, self.on_timer)
self.set_title("Fade example")
self.resize(350, 200)
self.set_position(Gtk.WindowPosition.CENTER)
self.connect("delete-event", Gtk.main_quit)
self.show_all()
def get_columns(self):
c1 = Gdk.Rectangle()
c1.x = c1.y = 30
c1.width = c1.height = 20
c2 = c1.copy()
c2.x = 120
c3 = c2.copy()
c3.x = 200
return (c1, c2, c3)
def on_timer(self):
if not self.timer: return False
self.darea.queue_draw()
return True
def on_draw(self, widget, cr):
columns = self.get_columns()
w, h = self.get_size()
cr.set_source_rgb(0.5, 0, 0)
cr.rectangle(0, 0, w, h)
cr.fill()
for rect in columns:
cr.set_source_rgb(0, 0.5, 0)
cr.rectangle(rect.x, rect.y, rect.width, rect.height)
cr.fill()
def on_draw_after(self, widget, cr):
columns = self.get_columns()
self.alpha += 0.08
for rect in columns:
cr.save()
pb = Gdk.pixbuf_get_from_surface(cr.get_target(), rect.x, rect.y, rect.width, rect.height)
cr.rectangle(rect.x, rect.y, rect.width, rect.height)
cr.set_source_rgb(0.5,0,0)
cr.fill()
Gdk.cairo_set_source_pixbuf(cr, pb, rect.x, rect.y)
cr.paint_with_alpha(self.alpha)
cr.restore()
print(self.alpha)
if self.alpha >= 1:
self.timer = False
app = FadeBoxWindow()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment