# Gtk2/cairo transparency question

**URL:** <https://www.ruby-forum.com/t/gtk2-cairo-transparency-question/203114>\
**Category:** Ruby-Gnome 2\
**Created:** [February 13, 2011, 5:30am UTC](https://www.ruby-forum.com/t/gtk2-cairo-transparency-question/203114 "2011-02-13T05:30:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 13, 2011, 5:30am UTC](https://www.ruby-forum.com/t/gtk2-cairo-transparency-question/203114/1 "2011-02-13T05:30:17Z")

</div>

hello folks -  
so i’m out of one problem and into another… first, thanks for  
replies to the last post, second -  
i’ve got a Gtk::Layout, in which i put a background image, and a  
Gtk::EventBox with a DrawingArea inside it. in the DrawingArea, i  
create a cairo context to draw (and redraw) a Pixbuf rotated relative to  
the time. thanks (again) to help from this forum, i’ve got everything  
working alright, but i can’t seem to make the EventBox (or maybe the  
DrawingArea?) transparent. the Pixbuf is from a .png, and responds  
“true” to #.has\_alpha?.  
here’s the code…  
#################################

require ‘gtk2’

class Spinner \< Gtk::EventBox

def initialize  
super  
@file = “/home/jk/ruby/arm.png”  
@da = Gtk::DrawingArea.new  
@da.signal\_connect(“expose-event”){|w, e| self.draw(w, e)}  
self.add(@da)  
self.set\_size\_request(600, 500)  
self.set\_visible\_window(false) # this doesn’t work…  
end #initialize

def draw(w, e)  
w.window.set\_opacity(0.1) # this doesn’t work either…  
cc = w.window.create\_cairo\_context  
time = Time.new  
angle = time.sec \* 6  
cc.translate(250, 250)  
cc.rotate(angle \* Math::PI / 180)  
pixbuf = Gdk::Pixbuf.new(@file)  
cc.set\_source\_pixbuf(pixbuf, -48, -46)  
cc.paint  
end #draw

def update  
@da.queue\_draw  
end #update

end #Spinner

spinner = Spinner.new  
win = Gtk::Window.new  
win.signal\_connect(“destroy”){Gtk.main\_quit}  
win.set\_size\_request(600, 600)  
layout = Gtk::Layout.new  
bgf = ("/home/jk/ruby/TradyBlix.png")  
bg = Gtk::Image.new(bgf)  
layout.put(bg, 100, 100)  
layout.put(spinner, 200, 200)  
layout.set\_size(600, 500)  
box = Gtk::VBox.new(false,0)  
btn = Gtk::Button.new(“update”)  
btn.signal\_connect(“clicked”){spinner.update}  
box.pack\_start(layout, true, true, 0)  
box.pack\_start(btn, false, false, 0)  
win.add(box)  
win.show\_all

Gtk.main  
###############################

any ideas? been searching, but haven’t come up with anything…

thanks -

j

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 13, 2011, 9:28am UTC](https://www.ruby-forum.com/t/gtk2-cairo-transparency-question/203114/2 "2011-02-13T09:28:43Z")

</div>

Hi,  
It’s not the image is not transparent, i’v put a smaller image in, and i  
see the large Gtk::Layout with my background-color on the larger image.  
So i guess what is really needed to tell the Gtk::Layout to have  
transparent background, or use something else for it.

Balint

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 13, 2011, 9:54am UTC](https://www.ruby-forum.com/t/gtk2-cairo-transparency-question/203114/3 "2011-02-13T09:54:20Z")

</div>

I’m resending this,  
Look what I’ve found:  
Note that “no window” widgets (which have the GTK\_NO\_WINDOW flag set)  
draw on their parent container’s window and thus may not draw any  
background themselves. This is the case for e.g. GtkLabel.  
(source [http://library.gnome.org/devel/gtk/unstable/GtkWidget.html](http://library.gnome.org/devel/gtk/unstable/GtkWidget.html) )  
So i replaced the Gtk::DrawingArea in your code to Gtk::Label, and my  
image is rendered with transparent background.

Balint

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 13, 2011, 9:58am UTC](https://www.ruby-forum.com/t/gtk2-cairo-transparency-question/203114/4 "2011-02-13T09:58:05Z")

</div>

Look what I’ve found:  
Note that “no window” widgets (which have the GTK\_NO\_WINDOW flag set)  
draw on their parent container’s window and thus may not draw any  
background themselves. This is the case for e.g. GtkLabel.  
(source [http://library.gnome.org/devel/gtk/unstable/GtkWidget.html](http://library.gnome.org/devel/gtk/unstable/GtkWidget.html) )  
So i replaced the Gtk::DrawingArea in your code to Gtk::Label, and my  
image is rendered with transparent background.

Balint

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 13, 2011, 2:28pm UTC](https://www.ruby-forum.com/t/gtk2-cairo-transparency-question/203114/5 "2011-02-13T14:28:40Z")

</div>

“Dobai-Pataky Bálint” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post #981366:

> So i replaced the Gtk::DrawingArea in your code to Gtk::Label, and my  
> image is rendered with transparent background.
> 
> Balint

hi Balint -  
very interesting - going to fool around with this some this afternoon,  
and i’ll get back to you.  
thanks for the idea…

-j

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 13, 2011, 3:19pm UTC](https://www.ruby-forum.com/t/gtk2-cairo-transparency-question/203114/6 "2011-02-13T15:19:44Z")

</div>

hi balint -

works like a charm… thanks!

- j
