如何使gtk.Layout透明 [英] how to make gtk.Layout transparent

查看:127
本文介绍了如何使gtk.Layout透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要害怕那个大班级 - 我写这样的文章很有趣,所以它可以通用的方式工作。它可能是透明元素的父级,而不是父级。
如此,麻烦。它可以使主透明gtk.Window,gtk.EventBox如下例所示,以及其他gtk.widgets,但它不适用于gtk.Layout,请帮助我。我想我写了足够的信息来发送问题。对不起我的英文:)

$ $ $ $ $ $ $ $ $ $ $ $# 8 - * -
导入gtk
导入cairo
类透明:
def __init __(self,* rgba):
Transparent.makeTransparent(self)
如果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)
尝试:
widget.rgba
,但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):
如果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)

带有事件框的工程:
eb = gtk.EventBox()
win.add(eb)
Transparent.makeTransparent(eb)

#但不包含布局:
#l = gtk.Layout(无,无)
#win.add(l)
#Transparent.makeTransparent(l)

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


解决方案

。我一直在学习这些东西,并且喜欢你的代码。
$ b $ p

从pygtk手册(加入强调):


gtk.Layout也可以绘制成类似于在gtk.DrawingArea上绘图。在处理gtk.Layout中的公开事件时,您必须绘制到由bin_window属性指定的窗口而不是窗口小部件窗口属性。

我认为在你的函数中,你得到的是window属性而不是用于cairo绘制的bin_window。



修改expose函数:

  @staticmethod 
def expose(widget,event):
如果'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
除了AttributeError:
widget.rgba =(0.0,0.0,0.0,0.0)
cr.set_source_rgba(* widget.rgba)
cr.rectangle(event.area)
cr.fill()


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, but it doesnot work with gtk.Layout, help me please. i think i wrote enough yet for send the question. sorry my English:)

#!/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()

解决方案

Very nice... I've been learning this stuff and I like your code.

From the pygtk manual (emphasis added):

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.

I think in your function you are getting the window attribute not the bin_window for cairo to draw on.

Modify the expose function to this:

@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()  

这篇关于如何使gtk.Layout透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆