2009-07-28 22 views
1

我想用Glade 3(gtk)和ruby制作一个gui,但无法弄清楚如何实际用一个字符串列表动态填充组合框。在我在Glade中直观地制作我的GUI之后,我得到了xml .glade文件,并使用ruby-glade-create-template生成我的.rb文件,但不知道该从哪里下载。我找不到任何有关如何使用Glade 3的红宝石的指南,任何帮助,将不胜感激。谢谢!如何使用/用Ruby/Glade填充组合框3

回答

0
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 

这是一些示例代码初始化一个组合框,是我用脚本名称来填补。

0

为什么你需要生成一个.rb文件?代码生成是Frowned Upon™。 那么你用一个Gtk::Builder实例加载glade文件?一旦你有了Builder对象,你可以调用get_object方法来获取特定的小部件。

这是关于glade 3的一个很好的教程,它不包括ruby,但python足够接近。它是过时了那么一点点了一些bug提到,如果您使用的是最新的林间空地的版本也出现了固定3.

0

如果使用visualruby,你可以创建一个这样的组合框:

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

出于某种原因,格莱德并不简单组合框工作。当您选择简单文本类型时,它不会在xml文件中正确保存。所以,你需要做的是在你希望组合框去的地方放置一个带有1个单元的VBox或HBox,并将上面的框添加到VBox中。在上面的例子中,@builder是Gtk :: Builder的一个实例。

还有更多的例子在:

http://visualruby.net