2011-02-03 5 views
6

저는 PyGTK를 사용하여 장식이없고 투명한 배경을 가진 창을 만들기 위해 열심히 노력했습니다. 나는 그 다음 카이로와 함께 창 내용을 그릴 것이다. 그러나 나는 그것을 작동시킬 수 없다.PyGTK와 PyCairo를 사용하여 창에서 투명한 배경을 얻는 방법?

나는이 작업을 수행하는 가장 간단한 방법은 무엇입니까,이 그들 그래서

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

중 하나입니다, 그들은 모두 실패, 다른 방법을 많이 시도했습니다?

답변

9

그래서, 실제로 이것을 직접 알아 냈습니다.

이것은 실제 작동하는 예입니다. 다른 사람이이 작업을 수행하는 방법에 관심이있는 경우를 대비하여 관련 부분에 대한 주석을 작성했습니다.

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

정확한 문제는 포럼에서 해결되었습니다. 하지만 C++에 있습니다. 이해해보십시오.

이 따르 Linux Questions

가 phorgan1에 의해 게시 된 주석을 참조하십시오. 호프가 도움이 되었기를 바랍니다 ....

+0

위 코드는 위의 코드와 매우 비슷합니다. C++ 코드에 따라 코드를 변경했지만 작동하지 않습니다. – paldepind

관련 문제