2009-07-28 31 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