Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a page to a Gtk Notebook widget at runtime?

Tags:

ruby

widget

gtk

I have the following code :


require "gtk2"

# adds a page to the notebook with the given label
def create_page(nb,label="untitled")
    # create a textview
    tx = Gtk::TextView.new
    # append it
    nb.append_page(tx,Gtk::Label.new(label))
end

Gtk.init
window = Gtk::Window.new
window.set_default_size(800,600)
window.signal_connect("destroy") {
    Gtk.main_quit
}

container   = Gtk::VBox.new
notebook    = Gtk::Notebook.new
button      = Gtk::Button.new("New")

# when I push the button, I want a new page to be added
button.signal_connect("clicked") {
    create_page(notebook)
}

container.pack_start(button,false,false,0)
create_page(notebook)
container.pack_start(notebook,true,true,0)
window.add(container)

window.show_all
Gtk.main

Basically, it's a window containing a button and a notebook widget. I want to be able to add a new page/tab to the notebook widget when I press the button. However, nothing happens. Is there a repaint of some sort I should do manually? Am I misusing the notebook widget? How can I add a tab at runtime?

like image 876
Geo Avatar asked Mar 23 '09 16:03

Geo


1 Answers

By replacing this :


button.signal_connect("clicked") {
    create_page(notebook)
}

with this:


button.signal_connect("clicked") {
    create_page(notebook)
    notebook.show_all
}

the newly added tabs/pages become visible.

like image 180
Geo Avatar answered Oct 31 '22 07:10

Geo