How do I remove widgets from main window

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

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.

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:

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

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

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

Thanks alot Martin =) Exactly what I needed.

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

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.

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

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.

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs