簡體   English   中英

透明小部件背景

[英]Transparent widget background

我是pygtk的新手。 我正在尋找一種方法來創建使用父backgorund作為其自身背景的自定義小部件,例如https://fbcdn-sphotos-ea.akamaihd.net/hphotos-ak-prn1/31617_421363141269410_1875576​​801_n.jpg

如何獲取父位圖並將其用作自己的位圖?

class RoundRectPanel(gtk.DrawingArea, PanelBase):
    """
    Panel That represents
    """
    def __init__(self):
        super(ProximityPanel, self).__init__()

    def initialize(self):
        super(RoundRectPanel, self).initialize()

        self.set_size_request(340, 300)
        self.connect('expose-event', self.expose)

    def terminate(self):
        pass

    def rounded_rectangle(self, cr, x, y, w, h, r=20):
        #   A****BQ
        #  H      C
        #  *      *
        #  G      D
        #   F****E

        cr.move_to(x+r,y)                      # Move to A
        cr.line_to(x+w-r,y)                    # Straight line to B
        cr.curve_to(x+w,y,x+w,y,x+w,y+r)       # Curve to C, Control points are both at Q
        cr.line_to(x+w,y+h-r)                  # Move to D
        cr.curve_to(x+w,y+h,x+w,y+h,x+w-r,y+h) # Curve to E
        cr.line_to(x+r,y+h)                    # Line to F
        cr.curve_to(x,y+h,x,y+h,x,y+h-r)       # Curve to G
        cr.line_to(x,y+r)                      # Line to H
        cr.curve_to(x,y,x,y,x+r,y)             # Curve to A

    def expose(self, canvas, event):
        # Create cairo context
        cr = canvas.window.cairo_create()

        # TODO: 1. GET PARENT background
        # 2. set it as canvas background

        # 3. draw on it
        self.rounded_rectangle(cr, 0, 0, 340, 300)
        cr.stroke_preserve()
        cr.set_source_rgba(1.0, 1.0, 1.0, 0.5)
        cr.fill()

當前找到了解決方法,該方法需要自定義窗口小部件才能與其父窗口(繪制背景圖像)進行通信。 CustomWidget需要來自父項的有關背景圖像的信息(文件名和窗口上的圖像位置)

class CustomWidget(gtk.DrawingArea):
def __init__(self, parent):
    super(CustomWidget, self).__init__()
    self.set_size_request(340, 300)
    self._parent = parent

    self.connect('expose-event', self._expose_event)

def _expose_event(self, widget, event):
    cr = widget.window.cairo_create()

    self.draw_context(cr, widget)

def draw_context(self, cr, widget):
    ctx = cr
    x, y, width, height = widget.allocation

    # parent window should have file name that is used as a background
    pixbuf = gtk.gdk.pixbuf_new_from_file(self._parent.file)
    ctx.save()
    # copy particular image region where your custom widget is located
    ctx.set_source_pixbuf(pixbuf, (-x) + self._parent.image.allocation.x, (-y) + self._parent.image.allocation.y)

    # draw background for custom widget
    ctx.paint()
    ctx.restore()

    # now draw what ever you want
    self.rounded_rectangle(cr, 0, 0, width, height)
    cr.set_line_width(1.0)
    cr.set_source_color(gtk.gdk.color_parse('grey'))
    cr.stroke_preserve()

    gradient = cairo.LinearGradient(width/2, 0, width/2, height)
    gradient.add_color_stop_rgba(0.00, 1, 1, 1, 0.9)
    gradient.add_color_stop_rgba(1.00, 0.4, 0.4, 0.4, 0.5)
    cr.set_source(gradient)
    cr.fill()

暫無
暫無

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

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