无法解决的Ruby Gtk2错误

时间:2009-09-22 05:47:42

标签: ruby gtk gtk2

我不断从我的代码中获得'未初始化的GLib :: Object'错误。

以下是代码:

class MainWindow < Gtk::Window
    def initialize
        title = "I'm The Title Of An Application! Yay!"
        border_width = 10
        set_size_request(600, 300)

        signal_connect("destroy") { Gtk.main_quit }

        @vbox = Gtk::VBox.new(false, 10)

        add(@vbox)
        show_all
    end
end

main_window = MainWindow.new

有了这个,我收到了错误

in `set_size_request': uninitialize GLib::Object

所以我将这些代码移到了课外,如下所示:

main_window = MainWindow.new
    main_window.set_size_request(600, 300)

然后我得到:

in `signal_connect': uninitialize GLib::Object

所以我把它改成了我的类,然后使用'set_resize_request'将'signal_connect'方法移到类外面,如下所示:

main_window = MainWindow.new
    main_window.set_size_request(600, 300)
    main_window.signal_connect("destroy") { Gtk.main_quit }

它给了我:

in `add': uninitialize GLib::Object

GWAR!我又改变了......

main_window = MainWindow.new
    main_window.set_size_request(600, 300)
    main_window.signal_connect("destroy") { Gtk.main_quit }
    main_window.add(@vbox)

现在这个?!

in `show_all': uninitialize GLib::Object

所以,再次,我从类中删除该方法并将其放在类

之外
main_window = MainWindow.new
    main_window.set_size_request(600, 300)
    main_window.signal_connect("destroy") { Gtk.main_quit }
    main_window.add(@vbox)
    main_window.show_all

也许......这次也许它会工作,所以我希望最好再次运行代码......

in `set_size_request': uninitialize GLib::Object

什么?这是怎么回事?

2 个答案:

答案 0 :(得分:4)

我认为你必须在初始化阶段之前先调用super

def initialize
  super
  # Other code goes here
  # ...
end

另外,请不要忘记添加Gtk.main

main_window = MainWindow.new
# Don't forget this...
Gtk.main

答案 1 :(得分:3)

除了khell的回答,你还应该注意你的前两行初始化:

    title = "I'm The Title Of An Application! Yay!"
    border_width = 10

创建两个从未使用过的局部变量。你可能希望在那里添加self.