Ruby Glade/gtkbuilder 示例?
我曾经使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这非常简单:只需使用 Glade 创建 GUI(并将其另存为 GtkBuilder),然后在 ruby 中使用它:
第一行创建 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:
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 callget_object(objname)
on builder, it will return the widget defined withobjname
).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 withcall
) from the block: that block will be invoked everytime the signal defined byhandler
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).你说得对,缺乏预先编写的教程,但 Ruby 中 gtkbuilder 的用法几乎与 Python 相同(相同的函数名称、调用顺序),因此这些可能会令人感兴趣 -
GTK::Builder 模块: http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A% 3ABuilder
Python代码:
来源: 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:
Source: http://www.micahcarrick.com/01-01-2008/gtk-glade-tutorial-part-3.html
另一个工作版本,采用面向对象的形式:
Another working version, in object oriented form:
确实是一样的。 这是一个空地示例: 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.