# Setting the Image of a Button

**URL:** <https://www.ruby-forum.com/t/setting-the-image-of-a-button/183089>\
**Category:** Ruby-Gnome 2\
**Created:** [February 7, 2010, 11:37pm UTC](https://www.ruby-forum.com/t/setting-the-image-of-a-button/183089 "2010-02-07T23:37:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![idrysdale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@idrysdale](https://www.ruby-forum.com/u/idrysdale)\
**Post date:** [February 7, 2010, 11:37pm UTC](https://www.ruby-forum.com/t/setting-the-image-of-a-button/183089/1 "2010-02-07T23:37:20Z")

</div>

Hello everyone,

I’m pretty new to both Ruby and Ruby-Gnome2. I am trying to create a GUI  
for TicTacToe and I’m running into some problems. I would like to be  
able to set the image of a button and then have it change its image when  
clicked. So far I have been unsuccessful at getting an image to even  
show up on a button. I am aware that it is possible to do this with the  
EventBox but I would really like to see it work with a Button. Here is  
the code that I am trying to use:

require ‘gtk2’

def change\_img(widget)  
image2 = Gtk::Image.new(“images/face-cool.png”)  
widget.image = image2  
end

image1 = Gtk::Image.new(“images/face-sad.png”)

button0 = Gtk::Button.new(“0”)  
button0.image = image1

#Attach signals  
button0.signal\_connect( “clicked” ) do |w|  
change\_img(w)  
end

box1 = Gtk::VBox.new(false, 0)  
box1.pack\_start(button0, false, false, 0)

window = Gtk::Window.new(“Button Images”)  
window.set\_default\_size(200,200)

window.signal\_connect(“delete\_event”) {  
false  
}

window.signal\_connect(“destroy”) {  
puts “destroy event occured”  
Gtk.main\_quit  
}

window.add(box1)  
window.show\_all  
Gtk.main

---

<div class="post-metadata">

**Author:** ![idrysdale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@idrysdale](https://www.ruby-forum.com/u/idrysdale)\
**Post date:** [February 7, 2010, 11:46pm UTC](https://www.ruby-forum.com/t/setting-the-image-of-a-button/183089/2 "2010-02-07T23:46:11Z")

</div>

Le dimanche 07 février 2010 à 23:37 +0100, Soil Soil a écrit :

> Hello everyone,
> 
> I’m pretty new to both Ruby and Ruby-Gnome2. I am trying to create a GUI  
> for TicTacToe and I’m running into some problems. I would like to be  
> able to set the image of a button and then have it change its image when  
> clicked. So far I have been unsuccessful at getting an image to even  
> show up on a button.

You code set the image fine here (and changes it when I click on the  
button)

---

<div class="post-metadata">

**Author:** ![idrysdale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@idrysdale](https://www.ruby-forum.com/u/idrysdale)\
**Post date:** [February 8, 2010, 2:07am UTC](https://www.ruby-forum.com/t/setting-the-image-of-a-button/183089/3 "2010-02-08T02:07:30Z")

</div>

Pascal T. wrote:

> Le dimanche 07 fï¿½vrier 2010 ï¿½ 23:37 +0100, Soil Soil a ï¿½crit :
> 
> > Hello everyone,
> > 
> > I’m pretty new to both Ruby and Ruby-Gnome2. I am trying to create a GUI  
> > for TicTacToe and I’m running into some problems. I would like to be  
> > able to set the image of a button and then have it change its image when  
> > clicked. So far I have been unsuccessful at getting an image to even  
> > show up on a button.
> 
> You code set the image fine here (and changes it when I click on the  
> button)

Hmmm thats wierd. I tried the code on my laptop, which is running Ubuntu  
9.04, and it runs just fine but on my desktop with Ubuntu 9.10 it  
doesn’t work. ☹

---

<div class="post-metadata">

**Author:** ![idrysdale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@idrysdale](https://www.ruby-forum.com/u/idrysdale)\
**Post date:** [February 8, 2010, 4:47am UTC](https://www.ruby-forum.com/t/setting-the-image-of-a-button/183089/4 "2010-02-08T04:47:14Z")

</div>

If anyone is running Ubuntu 9.10, could they pelase run the code and see  
if it works? I would like to know whether or not it is my machine or  
just the OS that is causing the problem. Thanks!

---

<div class="post-metadata">

**Author:** ![idrysdale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@idrysdale](https://www.ruby-forum.com/u/idrysdale)\
**Post date:** [February 8, 2010, 7:55am UTC](https://www.ruby-forum.com/t/setting-the-image-of-a-button/183089/5 "2010-02-08T07:55:43Z")

</div>

Ian D. wrote:

> If anyone is running Ubuntu 9.10, could they pelase run the code and see  
> if it works? I would like to know whether or not it is my machine or  
> just the OS that is causing the problem. Thanks!

I using Ubuntu 9.10 too and i don’t see any image (i changed the path of  
course). I also don’t see image on buttons when I’m using stocks (the  
text and  
mnemonic works). I’m using ruby 1.8.7 (2009-06-12 patchlevel 174)  
[x86\_64-linux] and ruby-gnome/ruby-gnome-dev 0.19-2ubuntu4.

I will try building form source later, how that will help.
