簡體   English   中英

如何使用PyGTK和PyCairo在窗口中獲得透明背景?

[英]How to get transparent background in window with PyGTK and PyCairo?

我一直在努力使用PyGTK創建一個沒有裝飾和透明背景的窗口。 然后我會用開羅繪制窗口的內容。 但我無法讓它發揮作用。

我嘗試了很多不同的方法,它們都失敗了,這就是其中之一

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

那么,最簡​​單的方法是什么?

所以,我實際上是自己想出來的。

這是一個有效的例子。 我已經評論了相關部分,以防其他人對如何做到這一點感興趣。

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

確切的問題已在論壇中得到解決。 但它是在C ++中。 試着理解這一點。

請關注: Linux問題

查看phorgan1發布的評論。 希望這可以幫助....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM