English 中文(简体)
PyGTK 透明窗口
原标题:PyGTK Transparent Window

我希望窗口透明, 但标签要在不透明中达到100% 。 我怎样才能做到这一点? BTW: 当我升级到 Ubuntu 12. 04 s Unity 界面时, 我注意到了那个窗口。 Set_ obvicity没有像 GNOME 那样工作, 但是即使它做到了, 窗口内的所有内容也会变得透明 。

这是我最初的代码...

#!/usr/bin/env python

import pygtk
pygtk.require( 2.0 )
import gtk
import pango
import time

class Clock:

  def __init__(self):
    me = gtk.Window(gtk.WINDOW_TOPLEVEL)
    me.connect("destroy", lambda w: gtk.main_quit())
    me.set_decorated(False)
    me.set_has_frame(False)
    me.set_resizable(False)
    me.set_property( skip-taskbar-hint , True)
    self.label = gtk.Label()
    self.label.modify_font(pango.FontDescription("FreeSerif Bold 50"))
    attr = pango.AttrList()
    fg_color = pango.AttrForeground(65535, 0, 0, 0, 65535)
    attr.insert(fg_color)
    self.label.set_attributes(attr)

    me.add(self.label)
    me.show_all()

  def update(self):
    self.label.set_text(time.strftime( %H:%M:%S ))
    return True

clock = Clock()
gtk.timeout_add(200, clock.update)
gtk.main()

我发现><这个 关于Tetubuntu的话题,

这是我的密码

#!/usr/bin/env python

import pygtk
pygtk.require( 2.0 )
import gtk
import pango
import time
import cairo

class Clock (gtk.Window):
  def __init__(self):
    super(Clock, self).__init__()
    self.connect("destroy", lambda w: gtk.main_quit())
    self.set_decorated(False)
    self.set_has_frame(False)
    self.set_resizable(False)
    self.set_property( skip-taskbar-hint , True)
    self.label = gtk.Label()
    self.label.modify_font(pango.FontDescription("FreeSerif Bold 50"))
    attr = pango.AttrList()
    fg_color = pango.AttrForeground(65535, 0, 0, 0, 65535)
    attr.insert(fg_color)
    self.label.set_attributes(attr)

    self.screen = self.get_screen()
    self.visual = self.screen.get_rgba_visual()
    self.set_visual(self.visual)
    self.set_app_paintable(True)
    self.connect("draw", self.area_draw)

    self.add(self.label)
    self.show_all()

  def update(self):
    self.label.set_text(time.strftime( %H:%M:%S ))
    return True

  def area_draw(self, widget, cr):
    cr.set_source_rgba(.2, .2, .2, 0.5)
    cr.set_operator(cairo.OPERATOR_SOURCE)
    cr.paint()
    cr.set_operator(cairo.OPERATOR_OVER)

clock = Clock()
gtk.timeout_add(200, clock.update)
gtk.main()
最佳回答

Well, you have to hack a lot to make a label like this. The best is to reinvent the wheel: make a round one.

Draw your own label. Get window context ( mywin.window.cairo_create())) and keep track of x, y.

def draw(*args):
    ctx = win.window.cairo_create()
    ctx.set_source_rgba(0, 0, 0, 0)
    ctx.set_operator(0) # OPERATOR_CLEAR
    ctx.paint()
    ctx.set_source_rgba(0, .6, 1, .3)
    ctx.arc(w/2, h/2, w/2, 0, 2*3.1415)
    ctx.fill()
    ctx.set_source_rgba(1, .8, 0, 1)
    ctx.show_text( 29-May-1234 ) #you have some work for font, style and so on

google gives you an hack that use ctypes for loading ttf font from file; it works very well on Linux( I have no idea on win). NOTE: Above asume that you have an composit manager, or ... rgba for widget is None. Also put an

colormap = self.win.get_screen().get_rgba_colormap()
if colormap == None: colormap = self.win.get_screen().get_rgb_colormap()
gtk.widget_set_default_colormap(colormap)

设置 rgba 如果可以的话, 并且它值得检查 :

if self.win.is_composited():print ( OK! )
else:self.bg = self.capt_screen()

...........

def capt_screen(self):
    x, y = self.win.get_position()
    win = gtk.gdk.get_default_root_window()
    w, h = win.get_size()
    pb = gtk.gdk.Pixbuf(0 , False, 8, w, h)
    self.win.hide()
    pb = pb.get_from_drawable(win, win.get_colormap(), 0, 0, 0, 0, w, h)
    self.win.show_all()
    if (pb != None):
        im = Image.fromstring( RGB , (w, h), pb.get_pixels())
        im = im.crop((x, y, x + self._w, y + self._h))
        im = im.convert( RGBA ) 
        return im #or pb

以上是“ xp” 透明效果: 复制 bg 和 bledit 与您的赢项。 只有当它不是一个 composit 管理器运行时。 部件可以使用 - 留在办公桌上, 但可以闪烁其它东西 : 每个刷新窗口都必须隐藏自己, 捕捉 gb 、 绘图并显示 。

PS: 我有一个工作时钟示例, 但我使用 png 。 如果您愿意, 我可以寄给您一个 tar. gz 邮件 。

问题回答

暂无回答




相关问题
Running jQuery command from modal window to affect main page

I m currently trying to write a jQuery script which opens a modal box then (upon user entry) changes a value on the original page. Currently I ve got the script working on just the page itself (...

Animate Window Resize (Width and Height) C# WPF

I m looking for some help on animating window resize of an open window! Cant seem to figure this one out! I m just using atm. this.Width = 500; Any help would be great! Thanks.

Cant drag and move a WPF Form

I design a WPF form with Window Style=None. So I Cannot see the drag bar in the form. How can I move the Form with WindowStyle=None Property?

Win32 CreateWindow() call hangs in child thread?

I m working on a portability layer for OpenGL (abstracts the glX and wgl stuff for Linux and Windows)... Anyhow, it has a method for creating a Window... If you don t pass in a parent, you get a real ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签