[英]How to get transparent background in window with PyGTK and PyCairo?
I've been trying really hard to create a window with no decoration and a transparent background using PyGTK. 我一直在努力使用PyGTK创建一个没有装饰和透明背景的窗口。 I would then draw the content of the window with Cairo. 然后我会用开罗绘制窗口的内容。 But I can't get it to work. 但我无法让它发挥作用。
I've tried a lot of different ways, they all failed, this is one of them 我尝试了很多不同的方法,它们都失败了,这就是其中之一
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk, sys, cairo
win = None
def expose (widget, event):
cr = widget.window.cairo_create()
#Start drawing
cr.set_operator(cairo.OPERATOR_CLEAR)
cr.set_source_rgba(0.5,1.0,0.0,0.5)
cr.rectangle(0, 0, 0.9, 0.8)
cr.fill()
def main (argc):
global win
win = gtk.Window()
win.set_decorated(False)
win.connect('delete_event', gtk.main_quit)
win.connect('expose-event', expose)
win.set_app_paintable(True)
win.show()
gtk.main()
if __name__ == '__main__':
sys.exit(main(sys.argv))
So, what is the simplest way to do this? 那么,最简单的方法是什么?
So, I actually figured this out myself. 所以,我实际上是自己想出来的。
This is a working example. 这是一个有效的例子。 I've commented the relevant parts just in case somebody else is interested in how to do this. 我已经评论了相关部分,以防其他人对如何做到这一点感兴趣。
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk, sys, cairo
from math import pi
def expose (widget, event):
cr = widget.window.cairo_create()
# Sets the operator to clear which deletes everything below where an object is drawn
cr.set_operator(cairo.OPERATOR_CLEAR)
# Makes the mask fill the entire window
cr.rectangle(0.0, 0.0, *widget.get_size())
# Deletes everything in the window (since the compositing operator is clear and mask fills the entire window
cr.fill()
# Set the compositing operator back to the default
cr.set_operator(cairo.OPERATOR_OVER)
# Draw a fancy little circle for demonstration purpose
cr.set_source_rgba(0.5,1.0,0.0,1)
cr.arc(widget.get_size()[0]/2,widget.get_size()[1]/2,
widget.get_size()[0]/2,0,pi*2)
cr.fill()
def main (argc):
win = gtk.Window()
win.set_decorated(False)
# Makes the window paintable, so we can draw directly on it
win.set_app_paintable(True)
win.set_size_request(100, 100)
# This sets the windows colormap, so it supports transparency.
# This will only work if the wm support alpha channel
screen = win.get_screen()
rgba = screen.get_rgba_colormap()
win.set_colormap(rgba)
win.connect('expose-event', expose)
win.show()
The exact problem has been addressed in a forum . 确切的问题已在论坛中得到解决。 But it is in C++ . 但它是在C ++中。 Try to understand that . 试着理解这一点。
Follow this : Linux Questions 请关注: Linux问题
See the comment posted by phorgan1 . 查看phorgan1发布的评论。 Hope this helps.... 希望这可以帮助....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.