不要害怕那个大类 - 对我来说,编写它很有趣,因此它可以以通用的方式工作。它可以是透明元素的父类,而不是父类。 所以,麻烦了。它可以使主 gtk.Window、gtk.EventBox(如下例所示)和其他 gtk.widgets 变得透明,但它不适用于 gtk.Layout,请帮助我。我想我已经写了足够多的内容来发送问题了。对不起我的英语:)
#!/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()
最佳答案
非常好...我一直在学习这些东西并且我喜欢你的代码。
摘自 pygtk 手册(已添加重点):
The gtk.Layout can also be drawn on similar to drawing on a gtk.DrawingArea. When handling expose events on a gtk.Layout, you must draw to the window specified by the bin_window attribute rather than the widget window attribute.
我认为在你的函数中你得到的是 window 属性,而不是 cairo 可以绘制的 bin_window 。
将暴露函数修改为:
@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()
关于python - 如何使 gtk.Layout 透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11568948/