Question

I am trying to make a gui with Glade 3 (gtk) and ruby but cannot figure out how to actually populate a combo box dynamically with say a list of strings. I get the xml .glade file after i visually make my gui in Glade, and use ruby-glade-create-template to generate my .rb file but have no idea where to go from here. I cannot find any guides on how to use ruby with Glade 3 and any help would be appreciated. Thanks!

Was it helpful?

Solution 2

def initialize_combobox_script_select()
    #get combobox widget from glade file
    @combobox_script_select = @glade.get_widget("combobox_script_select")
    @combobox_script_select.set_active(0) #this makes <select a script> the default element in the combobox
    @script_list = ["helloworld", "byeworld"]
    @script_list.each{|script| @combobox_script_select.append_text("#{script}")} #populate the combobox with the list of scripts
end

This is some example code to initialize a combobox, that i used to fill up with script names.

OTHER TIPS

Why do you need to generate a .rb file? Code generation is Frowned Upon™. So do you load the glade file with a Gtk::Builder instance? Once you have the Builder object you call the get_object method to get specific widgets.

Here is a pretty good tutorial on glade 3, it doesn't cover ruby but python is close enough. It is a little dated so some bugs mentioned there have been fixed if you are using a recent version of glade 3.

If you used visualruby, you can create a combobox like this:

combo = VR::SimpleComboBoxEntry("Selected", "Option1", "option2", "Option3")
@builder["vbox1"].add(combo)

For some reason, Glade doesn't work for simple comboboxes. When you select the simple text type, it doesn't save properly in the xml file. So, what you need to do is place a VBox or HBox with 1 cell into your glade form where you want the combobox to go, and add the above box to the VBox. In the above example, @builder is an instance of Gtk::Builder.

There are more examples at:

http://visualruby.net

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top