I'm trying to use glade3 with Ruby, but unfortunately examples of this are few and far between. Looking at what's available, I've come up with the following code, but there's still something missing, as the window does not display. There doesn't seem to be any GtkBuilder 'show' method, so I'm not sure what's needed to make it appear. Does anyone know what I need to do to make this work?
"hello.xml" is just a fairly simple glade3 xml file with a GtkWindow and a button.
#!/usr/bin/env ruby
require 'rubygems'
require 'gtk2'
class HelloGlade
attr :glade
def initialize
if __FILE__ == $0
Gtk.init
builder = Gtk::Builder::new
builder.add_from_file("hello.xml")
builder.connect_signals{ |handler| method(handler) } # (I don't have any handlers yet, but I will have eventually)
Gtk.main()
end
end
def gtk_main_quit
puts "Gtk.main_quit"
Gtk.main_quit()
end
end
hello = HelloGlade.new
hello.xml:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">butto开发者_Go百科n</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
</object>
</child>
</object>
</interface>
(edit)
Ugh, it's always the way. Post a question, and I figure out the answer for myself. I needed to get the window1 object from the GtkBuilder, and call its show() method:
...
window = builder.get_object("window1")
window.show()
Gtk.main
That program would be a lot easier to write if you used visualruby. This is what your code would look like:
class HelloGlade
include GladeGUI
def initialize
load_glade(__FILE__)
show_window()
end
end
You wouldn't need to set the "visible" property, or write code to close the window, or connect the signals. Everything is done automatically. There are plenty of examples here:
http://visualruby.net
精彩评论