#!/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()
答案 0 :(得分:3)
非常好......我一直在学习这些东西,我喜欢你的代码。
从pygtk手册(强调添加):
gtk.Layout也可以绘制为类似于gtk.DrawingArea上的绘图。处理gtk.Layout上的公开事件时,必须绘制到bin_window属性指定的窗口而不是窗口小部件窗口属性。
我认为在你的函数中,你得到的是window属性而不是bin_window,可以用来绘制cairo。
将公开函数修改为:
@staticmethod
def expose (widget, event):
if 'gtk.Layout' in str(type(widget)):
cr=widget.bin_window.cairo_create()
else:
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()