# How do I remove widgets from main window

**URL:** <https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700>\
**Category:** Ruby-Gnome 2\
**Created:** [October 8, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700 "2007-10-08T20:50:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [October 8, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/1 "2007-10-08T20:50:56Z")

</div>

I am trying to build a small program that loops trough and dynamically  
creates a number of buttons. Is there any methods available to remove  
already created widgets from the main window?  
Lets say I would like to remove one of the buttons below, how could I do  
that.

Thanks in advance

@buttons = {}  
10.times do |i|  
@buttons[“button#{i}”] Gtk::Button.new(“button #{i}”)  
end

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [October 8, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/2 "2007-10-08T21:24:14Z")

</div>

a Gtk::Window can hold only one widget since it’s a Gtk::Bin,  
so you used a Container when to which you added the Buttons.  
the Container which has an add(widget) method usually also have a  
remove(widget) method.

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [October 8, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/3 "2007-10-08T21:30:42Z")

</div>

Hi,

but remove only works for sub classes of Gtk::Container. The simplest  
way is to destroy the widget with your\_button.destroy. That will  
automaticaly remove the widget from its parent.

Cheers  
detlef

Am Montag, den 08.10.2007, 22:23 +0300 schrieb Dobai-Pataky BÃ¡lint:

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [November 22, 2007, 8:58pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/4 "2007-11-22T20:58:00Z")

</div>

Detlef R. wrote:

> Hi,
> 
> but remove only works for sub classes of Gtk::Container. The simplest  
> way is to destroy the widget with your\_button.destroy. That will  
> automaticaly remove the widget from its parent.
> 
> Cheers  
> detlef

I have put all my buttons in a vBox and I would like to be able to  
delete them.  
How is this possible?

dubbyd

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [November 28, 2007, 8:43am UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/5 "2007-11-28T08:43:12Z")

</div>

tobbyd dubbyd wrote:

> I dont really get it. I have packed my buttons i a vBox but perhaps I  
> should have used something else. If someone could provide a snippet with  
> 3 buttons packet in some container and at the click on one of the  
> buttons another button should be removed/deleted, I would be very  
> thankful.

# does this help?

require ‘gtk2’  
Gtk.init

win = Gtk::Window.new

button1 = Gtk::Button.new(“button 1”)  
button2 = Gtk::Button.new(“button 2”)  
button3 = Gtk::Button.new(“button 3”)

hb = Gtk::HBox.new(false)

hb.pack\_start( button1 )  
hb.pack\_start( button2 )  
hb.pack\_start( button3 )

button1.signal\_connect(“clicked”){  
hb.remove( button2 )  
}

# win.add( hb ) win.show\_all Gtk.main

Note that only the first click will work properly. The second (and so  
on)  
click will not do anything, and cause GTK to fire a (non fatal)  
assertion.

martin

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [November 27, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/6 "2007-11-27T21:44:57Z")

</div>

I dont really get it. I have packed my buttons i a vBox but perhaps I  
should have used something else. If someone could provide a snippet with  
3 buttons packet in some container and at the click on one of the  
buttons another button should be removed/deleted, I would be very  
thankful.

dubbyd

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [November 29, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/7 "2007-11-29T20:46:08Z")

</div>

Thanks alot Martin =) Exactly what I needed.

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [November 29, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/8 "2007-11-29T23:46:11Z")

</div>

Did not take very long until next problem showed up =(  
When I click my button I would like to create a new slot consisting of  
one Entry and a normal button. So when myButton is clicked the function  
below is running and it is supposed to create only one slot.  
What happens is that the first time I click myButton one slot is created  
and everything is fine. Then when I want to create one more, two slots  
are created. Next time three and so on but what I want is only one new  
slot per click. Why is it increased and how do I get rid of it?  
Thanks

def addSlotCallBack( myButton )

```
    e = e + 1

```

@Entrys[“entry#{e}”] = Gtk::Entry.new  
@Entrys[“entry#{e}”].set\_text(“Entry#{e}”)  
@Entrys[“entry#{e}”].show

#dynamic buttons  
@buttons[“button#{e}”] = Gtk::Button.new(“button#{e}”)  
@buttons[“button#{e}”].show

@hBoxes[“hBox#{e}”] = Gtk::HBox.new( false, 10 )  
@hBoxes[“hBox#{e}”].pack\_start( @buttons[“button#{e}”], true, true, 0  
)  
@hBoxes[“hBox#{e}”].pack\_start(@Entrys[“entry#{e}”], true, true, 0 )  
@hBoxes[“hBox#{e}”].show

```
    #pack rows of buttons in to VBox
   $vBox.pack_start( @hBoxes["vBox#{e}"], true, true, 0 )

```

end

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [November 30, 2007, 10:33am UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/9 "2007-11-30T10:33:12Z")

</div>

tobbyd dubbyd wrote:

> def addSlotCallBack( myButton )  
> @buttons[“button#{e}”].show
> 
> @hBoxes[“hBox#{e}”] = Gtk::HBox.new( false, 10 )  
> @hBoxes[“hBox#{e}”].pack\_start( @buttons[“button#{e}”], true, true, 0  
> )  
> @hBoxes[“hBox#{e}”].pack\_start(@Entrys[“entry#{e}”], true, true, 0 )  
> @hBoxes[“hBox#{e}”].show
> 
> ```
> #pack rows of buttons in to VBox
> $vBox.pack_start( @hBoxes["vBox#{e}"], true, true, 0 )
> 
> ```

typo here ^^^^^

you’ve created @hBoxes[“hBox#{e}”] but you’re trying to show the entry  
called “vBox#{e}”, which I guess is lying around elsewhere…

Gtk style is to create a widget, put it in its container, and _then_  
show it.  
in the above you can simplify things by removing all the .show lines and  
having a  
@hBoxes[“hBox#{e}”].show\_all at the end.

martin.

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [November 30, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/10 "2007-11-30T22:03:42Z")

</div>

Thanks for the input Martin and that “VBox” thing was actually a type  
error I made yesterday, sorry bout that. The function below is now  
exactly the way it is in my program.  
My problem remains though. When I click my button that runs this  
function the first time it creates a hbox with an entry and a button  
just as it is supposed to. The problem is next time I press the button  
then it creates two hBoxes with corresponding buttons etc. The next time  
4 and so on and I just can not figure out why. I just want it to create  
on hBox per click. Even if I set e = constant this still happens?

def addSlotCallBack( widget )

$rowsTotal = $rowsTotal + 1  
e = $rowsTotal

@Entrys[“entry#{e}”] = Gtk::Entry.new  
@Entrys[“entry#{e}”].set\_text(“Entry#{e}”)

```
    #dynamic buttons

```

@buttons[“button#{e}”] = Gtk::Button.new(“button#{e}”)  
createPickUpButtonStates(@buttons[“button#{e}”],e,@Entrys[“entry#{e}”])

@hBoxes[“hBox#{e}”] = Gtk::HBox.new( false, 10 )  
@hBoxes[“hBox#{e}”].pack\_start( @buttons[“button#{e}”], true, true, 0  
)  
@hBoxes[“hBox#{e}”].pack\_start(@Entrys[“entry#{e}”], true, true, 0 )  
@hBoxes[“hBox#{e}”].show\_all

#pack rows of buttons in to VBox

```
  $vBox.pack_start( @hBoxes["hBox#{e}"], true, true, 0 )

```

end

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [December 3, 2007, 9:54am UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/11 "2007-12-03T09:54:11Z")

</div>

tobbyd dubbyd wrote:

> def addSlotCallBack( widget )

could you include the code which sets up the callback for the button.  
i suspect the problem is around there as the code you’ve shown  
so far looks ok.

martin.

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [December 20, 2007, 7:36am UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/12 "2007-12-20T07:36:18Z")

</div>

Hmm i think it might be even better to include the whole sample… 😉

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [December 19, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/how-do-i-remove-widgets-from-main-window/116700/13 "2007-12-19T20:59:18Z")

</div>

Better late than never =)

def addSlotCallBack( widget )

puts “Hello again - #{widget} was pressed.”

```
  $rowsTotal = $rowsTotal + 1
  e = $rowsTotal

```

@Entrys[“entry#{e}”] = Gtk::Entry.new  
@Entrys[“entry#{e}”].set\_text(“Entry#{e}”)  
#@Entrys[“entry#{e}”].show\_all

#dynamic buttons  
@buttons[“button#{e}”] = Gtk::Button.new(“button#{e}”)  
createPickUpButtonStates(@buttons[“button#{e}”],  
e,@Entrys[“entry#{e}”])  
#@buttons[“button#{e}”].show\_all

@hBoxes[“hBox#{e}”] = Gtk::HBox.new( false, 10 )  
@hBoxes[“hBox#{e}”].pack\_start( @buttons[“button#{e}”], true, true, 0  
)  
@hBoxes[“hBox#{e}”].pack\_start(@Entrys[“entry#{e}”], true, true, 0 )  
@hBoxes[“hBox#{e}”].show\_all

#pack rows of buttons in to VBox  
$vBox.pack\_start( @hBoxes[“hBox#{e}”], true, true, 0 )

puts “rows are”,$rowsTotal

end
