Ruby Glade/gtkbuilder 示例?

发布于 2024-07-25 10:31:41 字数 119 浏览 6 评论 0原文

我曾经使用ruby和glade2来设计用户界面一段时间,在新版本的glade3中我可以使用gtkbuilder格式来生成xml文件而不是libglade。

有什么例子吗? 我搜索了谷歌,但我没有运气!

I was using ruby and glade2 to design the user interface for a while in the new version of glade3 i can use gtkbuilder format to generated xml file instead of libglade.

is there any example? i searched google but i had no luck!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

这非常简单:只需使用 Glade 创建 GUI(并将其另存为 GtkBuilder),然后在 ruby​​ 中使用它:

require 'rubygems'
require 'gtk2'
# require 'libglade2' #you don't need this anymore

builder = Gtk::Builder.new
builder.add_from_file(file)
builder.connect_signals {|handler| method(handler) }

第一行创建 Builder 对象,该对象负责创建 Glib::Objects来自您的 xml 定义,并存储它们以供以后使用(您可以在构建器上调用 get_object(objname) ,它将返回使用 objname 定义的小部件)。

第二行实际上加载您的接口定义,其中 file 是 gtkbuilder 文件的路径。

第三行有点晦涩难懂。 connect_signals 为您在接口中定义的每个信号调用一次提供的块。 handler 只是一个字符串(信号的名称)。 您应该从块返回一个过程(或任何可通过 call 调用的内容):每次触发 handler 定义的信号时都会调用该块。 在此示例中,块仅返回一个与信号同名的方法(并且,为了简单起见,假设接口中定义的每个信号都有一个方法)。

It's really simple: just create your GUI with Glade (and save it as GtkBuilder) and then use it in ruby with:

require 'rubygems'
require 'gtk2'
# require 'libglade2' #you don't need this anymore

builder = Gtk::Builder.new
builder.add_from_file(file)
builder.connect_signals {|handler| method(handler) }

the first line creates the Builder object, which is responsible of creating the Glib::Objects from your xml definition and also stores them for later use (you can call get_object(objname) on builder, it will return the widget defined with objname).

The second line actually loads your interface definition, where file is the path to your gtkbuilder file.

The third line is somewhat more obscure. connect_signals calls the block provided once for every signal you have defined in your interface. handler is just a string (the name of the signal). You are supposed to return a proc (or anything invokable with call) from the block: that block will be invoked everytime the signal defined by handler is fired. In this example the block is just returning a method with the same name as the signal (and, for simplicity's sake, it is assumed that there is a method for every one of the signals defined in the interface).

怀中猫帐中妖 2024-08-01 10:31:41

你说得对,缺乏预先编写的教程,但 Ruby 中 gtkbuilder 的用法几乎与 Python 相同(相同的函数名称、调用顺序),因此这些可能会令人感兴趣 -

GTK::Builder 模块: http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A% 3ABuilder

Python代码:

import sys
import gtk

class TutorialTextEditor:

    def on_window_destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):

        builder = gtk.Builder()
        builder.add_from_file("tutorial.xml") 

        self.window = builder.get_object("window")
        builder.connect_signals(self)       

if __name__ == "__main__":
    editor = TutorialTextEditor()
    editor.window.show()
    gtk.main()

来源: http://www.micahcarrick.com/01-01-2008/gtk-glade-tutorial-part-3.html

You're right on the lack of pre-written tutorials, but the usage of gtkbuilder in Ruby is almost identical to Python (same function names, call orders) so these might be of interest -

GTK::Builder module: http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ABuilder

Python code:

import sys
import gtk

class TutorialTextEditor:

    def on_window_destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):

        builder = gtk.Builder()
        builder.add_from_file("tutorial.xml") 

        self.window = builder.get_object("window")
        builder.connect_signals(self)       

if __name__ == "__main__":
    editor = TutorialTextEditor()
    editor.window.show()
    gtk.main()

Source: http://www.micahcarrick.com/01-01-2008/gtk-glade-tutorial-part-3.html

栀梦 2024-08-01 10:31:41

另一个工作版本,采用面向对象的形式:

require 'gtk2'

class Builder < Gtk::Builder

def initialize xml
        super()

        self.add_from_string(xml)

    self['main'].set_window_position Gtk::Window::POS_CENTER
        self['main'].signal_connect('destroy') { Gtk.main_quit }
        self['main'].show_all

    self.connect_signals{ |handler| method(handler) }

end

def on_button_clicked w

    case w.label
            when 'quit'
                Gtk::main_quit
            else
                puts "# on_button_clicked : " + w.label

    end
    end

def on_main_destroy
        puts "# on_main_destroy"
        Gtk::main_quit
    end
end

if __FILE__ == $0

xml = <<EOI
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <object class="GtkWindow" id="main">
    <property name="can_focus">False</property>
    <child>
       <object class="GtkButton" id="quit">
        <property name="label">quit</property>
        <signal name="clicked" handler="on_button_clicked" swapped="no"/>
      </object>
    </child>
  </object>
</interface>
EOI


    Gtk.init
        builder = Builder.new(xml)
    Gtk.main

end

Another working version, in object oriented form:

require 'gtk2'

class Builder < Gtk::Builder

def initialize xml
        super()

        self.add_from_string(xml)

    self['main'].set_window_position Gtk::Window::POS_CENTER
        self['main'].signal_connect('destroy') { Gtk.main_quit }
        self['main'].show_all

    self.connect_signals{ |handler| method(handler) }

end

def on_button_clicked w

    case w.label
            when 'quit'
                Gtk::main_quit
            else
                puts "# on_button_clicked : " + w.label

    end
    end

def on_main_destroy
        puts "# on_main_destroy"
        Gtk::main_quit
    end
end

if __FILE__ == $0

xml = <<EOI
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <object class="GtkWindow" id="main">
    <property name="can_focus">False</property>
    <child>
       <object class="GtkButton" id="quit">
        <property name="label">quit</property>
        <signal name="clicked" handler="on_button_clicked" swapped="no"/>
      </object>
    </child>
  </object>
</interface>
EOI


    Gtk.init
        builder = Builder.new(xml)
    Gtk.main

end
动听の歌 2024-08-01 10:31:41

确实是一样的。 这是一个空地示例: http://snippets.dzone.com/posts/show/5251< /a> 替换为正确的方法即可。

有一个用 Ruby 编写的 IDE: http://github.com/danlucraft/redcar/tree/ master 但我无法找到它的主文件来查看它是否使用构建器。

It is the same really. Here is a glade example: http://snippets.dzone.com/posts/show/5251 substitute with the proper methods and you're set.

There is an IDE written in Ruby: http://github.com/danlucraft/redcar/tree/master but I wasn't able to find it's main file to begin with to see if it uses builder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文