문제

I want the window transparent, but the label to be 100% in opacity. How can I achieve this? BTW: I noticed when I upgraded to Ubuntu 12.04's unity interface that window.set_opacity wasn't working like it did on GNOME, but even if it did all the content inside the window would become transparent as well.

This is the code I started out with...

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

I found this topic on askubuntu and It's exactly what I was looking for however now I'm having problems having the digital clock show. Any help would be greatly appreciated.

Here's my code.

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

to set rgba if posible, and also it worth a check:

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

The above is „xp” transparent efect: copy the bg and bledit with your win. Only if it is not a composit manager runing. Is ok for widget - stay on desk, but flickr for something else: at every refresh window has to hide itself, capture the gb, drawing and reveal.

PS: I have an working clock example, but I use png. If you like, i could send you an tar.gz on mail

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top