четверг, 9 августа 2012 г.

How to make gtk.Widget (and Layout particularly) transparent.

Don be afraid of that big class - it was interesting for me to write so it could work in that universal way. It could be the parent-class for transparent elements, and not-parent. so, the trouble. it can make transparent the main gtk.Window, the gtk.EventBox as in example below, and other gtk.widgets


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gtk
import cairo
class Transparent:
    def __init__(self,*rgba):
        Transparent.makeTransparent(self)
        if len(rgba)>0:
            self.rgba=rgba[0]
    @staticmethod
    def expose (widget, event):
        cr = widget.window.cairo_create()
        cr.set_operator(cairo.OPERATOR_CLEAR)
        cr.rectangle(event.area)
        cr.fill()
        cr.set_operator(cairo.OPERATOR_OVER)
        try:
            widget.rgba
        except AttributeError:
            widget.rgba=(0.0,0.0,0.0,0.0)
        cr.set_source_rgba(*widget.rgba)
        cr.rectangle(event.area)
        cr.fill()  
    @staticmethod
    def makeTransparent(thing,*rgba): 
        if len(rgba)>0:
            thing.rgba=rgba[0]   
        thing.expose=Transparent.expose
        thing.set_app_paintable(True)
        screen = thing.get_screen()
        rgba = screen.get_rgba_colormap()
        thing.set_colormap(rgba)
        thing.connect('expose-event', thing.expose)

win = gtk.Window()
Transparent.makeTransparent(win)

#works with EventBox:
eb=gtk.EventBox()
win.add(eb)
Transparent.makeTransparent(eb)

#but not with Layout:
#l=gtk.Layout(None,None)
#win.add(l)
#Transparent.makeTransparent(l)

win.show_all()
win.show()
gtk.main()

About how to apply it for Layout - read there

Комментариев нет:

Отправить комментарий