# Refresh Gtk::Image (newb question)

**URL:** <https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165>\
**Category:** Ruby-Gnome 2\
**Created:** [May 25, 2010, 9:17pm UTC](https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165 "2010-05-25T21:17:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lljk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@lljk](https://www.ruby-forum.com/u/lljk)\
**Post date:** [May 25, 2010, 9:17pm UTC](https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165/1 "2010-05-25T21:17:22Z")

</div>

hello all,  
first let me say that i’m a complete programming and ruby newbie -  
haven’t done any programming since writing stuff in basic on my TI99-4a,  
circa 1984 - so please forgive any questions or code (!) that seem  
ridiculous.  
here’s my story/questions…  
i’m working on a simple mp3 player written in ruby, using gstreamer  
and gtk+… really pretty straightforward - not supposed to do much  
except load and save a playlist, and show some playback controls and the  
cover art. i’ve got it doing all of that alright, only problem is i  
can’t figure out for the life of me how to update the album art when the  
song changes, either from a ‘next’ button click, or reaching the end of  
the song. i’ve googled the b-jaysus out of refreshing gtk images, and  
come up with some different ideas (event boxes, drawing areas…) but i  
can’t seem to get anything to work.  
i imagine this is something really simple that for some reason i just  
can’t figure out. it seems that i have to jump out of the gtk.main loop  
in order to update the cover. i know that a Gdk (that’s “d,” not “t,”)  
window has an invalidate method, and a queue method, and it seems like  
that could be a solution - or maybe through triggering an eventBox…  
or through some other blatently obvious solution that i have no idea  
about.  
here’s a look at what i think should be the relevant code… thanks in  
advance for any and all help.

everything is inside a Gtk::Window - here’s the bit about the album  
art…

code snippet:  
…

## “player.img” is dynamic, based on the current song

coverpix = Gdk::Pixbuf.new(player.img, 196, 196)  
cover = Gtk::Image.new(coverpix)  
cover.show  
artBox = Gtk::EventBox.new.add(cover)  
artBox.show

box = Gtk::VBox.new(false,0)  
box.pack\_start(artBox,true,true,2)  
box.pack\_start(bbox,false,false,2)  
box.show

window.add box  
window.show

Gtk.main  
…

i’ve tried using this bit of code as a ‘refresh’ method, and also  
inside the next button’s EventBox block - when i add a “puts player.img”  
in there to see what’s happening under the hood, i don’t see anything  
until i stop the program (breaking the gtk main loop,) at which point  
any and all of the correct image filenames appear for the songs played.

again, sorry if this is a boneheaded question, but i’ve been up high  
down and low searching and experimenting with no luck - any ideas would  
be appreciated…

-jk

---

<div class="post-metadata">

**Author:** ![lljk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@lljk](https://www.ruby-forum.com/u/lljk)\
**Post date:** [May 25, 2010, 11:20pm UTC](https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165/2 "2010-05-25T23:20:24Z")

</div>

Hello

I suspect that the issue you are experiencing stems from Gtk::Image  
widget not updating on changes to the Gdk::Image it is displaying.

The simplest way to work around this is to create a new Gdk::Image  
when the cover art changes and assign it to the Gtk::Image widget.

The other way would be to invalidete the widget so that it gets  
repainted eventually or force it to repaint even.

If your problem is not solved by this then I suggest you post the  
exact code which demonstrates it.

HTH

Michal

---

<div class="post-metadata">

**Author:** ![lljk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@lljk](https://www.ruby-forum.com/u/lljk)\
**Post date:** [May 25, 2010, 11:34pm UTC](https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165/3 "2010-05-25T23:34:27Z")

</div>

Hi.

I have had the same issue with my music player. You just call the  
Gtk::Image#show each time the image is updated.

Vincent

P.S.: on your snippet, you don’t have to call the show method on all  
Gtk widgets instead you can call show\_all on the main container once  
other widgets have been added (i.e. window.show\_all instead of all the  
show methods)

2010/5/25, Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![lljk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@lljk](https://www.ruby-forum.com/u/lljk)\
**Post date:** [May 26, 2010, 12:45pm UTC](https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165/4 "2010-05-26T12:45:24Z")

</div>

hi folks -

thank you all very much indeed - now i’ve got some things to play  
with! going to fiddle around some with all of your suggestions and see  
what i can come up with. if i can’t get things working i’ll post back  
with more complete code, and when i do get it working i’ll make sure to  
post the solution that worked for me.

thanks again,  
viva the ruby forum!

jk

---

<div class="post-metadata">

**Author:** ![lljk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@lljk](https://www.ruby-forum.com/u/lljk)\
**Post date:** [May 26, 2010, 12:48am UTC](https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165/5 "2010-05-26T00:48:12Z")

</div>

Hello!

Take a look at:  
[http://www.raditex.nu//websvn/listing.php?repname=kurssrc.Exempel&path=%2FRubyStateMachine%2F#\_RubyStateMachine\_](http://www.raditex.nu//websvn/listing.php?repname=kurssrc.Exempel&path=%2FRubyStateMachine%2F#_RubyStateMachine_)

First start “ruby ./DoorServer.rb” And then “ruby ./GtkDoorClient.rb”

1. This implement a simple state machine

2. I demonstrate how you could split your applications in client  
server…

GÃ¶ran

2010/5/25 Jake K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lljk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@lljk](https://www.ruby-forum.com/u/lljk)\
**Post date:** [May 26, 2010, 11:55pm UTC](https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165/6 "2010-05-26T23:55:34Z")

</div>

…the eagle has landed.

thanks all…

j

---

<div class="post-metadata">

**Author:** ![lljk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@lljk](https://www.ruby-forum.com/u/lljk)\
**Post date:** [May 26, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165/7 "2010-05-26T20:26:01Z")

</div>

hello again,

first let me thank you all for your suggestions, they got me thinking  
and experimenting, and i’ve now got some code working in an example  
program that i think will solve my problem. i ended up clearing the  
EventBox with the #remove method, then recreating the Gtk::Image as  
suggested, and then adding this to the now empty EventBox. not sure how  
‘elegant’ it is, but it works!  
i’ve included the code to the sample program - feel free to let me  
know if you think something looks just plain dumb! i’ll give a quick  
shout back when i get this worked into the mp3 player.

thanks again for the ideas…

peace, love, rock and roll -

jk

########## CODE #######################################

require ‘gtk2’

class Pics  
attr\_accessor :pile, :picindex, :atBat, :image, :box, :window

def initialize  
@window = Gtk::Window.new()  
@window.signal\_connect(“destroy”){Gtk.main\_quit}  
pic1 = “TradyBlix.png”  
pic2 = “ride.png”  
pic3 = “konichiwa.jpeg”  
@pile = [pic1, pic2, pic3]  
@picindex = 0  
self.getImage  
@box = Gtk::EventBox.new.add(@image)  
end

def batterUp  
@box.remove(@image)  
@picindex = @picindex + 1  
@picindex = 0 if @picindex == @pile.length  
self.getImage  
@box.add(@image)  
@box.show  
end

def getImage  
@atBat = @pile[@picindex]  
img = Gdk::Pixbuf.new(@atBat, 200, 200)  
@image = Gtk::Image.new(img)  
@image.show  
end

end # class Pics

pics = Pics.new  
pics.box.signal\_connect(“button\_press\_event”){pics.batterUp}  
pics.window.set\_default\_size(200, 250)  
pics.window.add(pics.box)  
pics.window.show\_all

Gtk.main

############ EOF ##############################

---

<div class="post-metadata">

**Author:** ![lljk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@lljk](https://www.ruby-forum.com/u/lljk)\
**Post date:** [May 27, 2010, 9:43am UTC](https://www.ruby-forum.com/t/refresh-gtk-image-newb-question/189165/8 "2010-05-27T09:43:10Z")

</div>

On 26 May 2010 20:26, jake kaiden [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> hello again,
> 
> Â first let me thank you all for your suggestions, they got me thinking  
> and experimenting, and i’ve now got some code working in an example  
> program that i think will solve my problem. Â i ended up clearing the  
> EventBox with the #remove method, then recreating the Gtk::Image as  
> suggested, and then adding this to the now empty EventBox. Â not sure how  
> ‘elegant’ it is, but it works!  
> Â i’ve included the code to the sample program - feel free to let me  
> know if you think something looks just plain dumb! Â i’ll give a quick

As I wrote earlier it suffices to replace the Gdk::Pixbuf, the  
GtkImage can stay:

@@ -7,11 +7,9 @@  
def initialize  
@window = Gtk::Window.new()  
@window.signal\_connect(“destroy”){Gtk.main\_quit}

- pic1 = “TradyBlix.png”
- pic2 = “ride.png”
- pic3 = “konichiwa.jpeg”
- @pile = [pic1, pic2, pic3]

- @pile = Dir::glob(“104\*”) + Dir::glob(“2003\*”)  
@picindex = 0
- @image = Gtk::Image.new  
self.getImage  
@box = Gtk::EventBox.new.add(@image)  
end  
@@ -28,8 +26,7 @@  
def getImage  
@atBat = @pile[@picindex]  
img = Gdk::Pixbuf.new(@atBat, 200, 200)

- @image = Gtk::Image.new(img)
- @image.show

- @image.pixbuf = img  
end

end # class Pics

HTH

Michal
