我有一个Python 2.7和PyGTK的项目。
我需要创建一个透明的背景窗口,但仍然能够显示图像(像素图和蒙版为基础)和窗口内的其他对象。
我正在使用以下代码,但没有图像对象显示在Ubuntu (Oneric Ocelot),我得到一个错误,张贴在下面,(虽然窗口否则显示,其按钮对象)。这甚至不能在Windows 7中渲染(这个错误也在下面发布)。
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)
hab_fish_win = gtk.Window()
hab_fish_win.resize(640, 480)
hab_fish_win.set_resizable(False)
hab_fish_win.set_decorated(False)
hab_fish_win.set_has_frame(False)
hab_fish_win.set_position(gtk.WIN_POS_CENTER)
hab_fish_win.set_app_paintable(True)
screen = hab_fish_win.get_screen()
rgba = screen.get_rgba_colormap()
hab_fish_win.set_colormap(rgba)
hab_fish_win.connect('expose-event', expose)
hab_fish_win.show()
WINDOWS 7 RUN:
回溯(最近一次调用):文件"C:UsersuserMousePaw . C"GamesWord4WordPYMfishtest2.py",第337行HAB_FISH()文件"C:UsersuserMousePaw GamesWord4WordPYMfishtest2.py",第100行,在inithab_fish_win.set_colormap(rgba) TypeError: Gtk.Widget.set_colormap()参数1必须是gtk.gdk。Colormap,不是没有一个
快速"print rgba"确认rgba为"None",因此出现错误。
UBUNTU "ONERIC OCELOT" RUN:
Gtk警告:试图将深度为24的可绘制对象绘制到深度为32的可绘制对象
怎么回事?我非常需要透明的背景
好吧,经过几个小时的研究,我发现Windows不支持这种透明度。至于Linux的错误,我不知道。
我正在移植到PyGObject,并使用另一种方法来实现我的目的。我建议这个答案的读者仔细研究一下。