从使用 libglade 转换为 GtkBuilder 有哪些步骤? (Python)
我有一个使用 libglade 的小项目,并使用以下内容加载 xml 文件:
self.gladefile = "sdm.glade"
self.wTree = gtk.glade.XML(self.gladefile)
self.window = self.wTree.get_widget("MainWindow")
if (self.window):
self.window.connect("destroy", gtk.main_quit)
dic = { "on_button1_clicked" : self.button1_clicked,
"on_MainWindow_destroy" : gtk.main_quit}
self.wTree.signal_autoconnect(dic)
在 Glade 中转换我的项目后,我需要进行哪些结构更改? 我使用的是 Ubuntu 9.04。
I have a small project that uses libglade and use the following to load the xml file:
self.gladefile = "sdm.glade"
self.wTree = gtk.glade.XML(self.gladefile)
self.window = self.wTree.get_widget("MainWindow")
if (self.window):
self.window.connect("destroy", gtk.main_quit)
dic = { "on_button1_clicked" : self.button1_clicked,
"on_MainWindow_destroy" : gtk.main_quit}
self.wTree.signal_autoconnect(dic)
After converting my project in glade, what structural changes do I need to make?
I'm on Ubuntu 9.04.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
gtk.Builder
来代替。 此类可以加载任意数量的 UI 文件,因此您需要以文件或字符串的形式手动添加它们:只需在构建器类上使用
get_object
即可代替get_widget
:要连接信号,只需使用
connect_signals
,它也需要一个字典:过去的情况(至少在 GTK+ 2.12 中,不确定是否仍然相同)可以调用
connect_signals
仅一次,第一次调用期间未连接的任何信号将永远不会连接。 这在林间空地有所不同,所以如果您之前依赖过该功能,请务必小心。You need to use
gtk.Builder
instead. This class can load any number of UI files, so you need to add them manually, either as files or as strings:Instead of
get_widget
, just useget_object
on the builder class:To connect the signals, just use
connect_signals
, which also takes a dictionary:It used to be the case (at least in GTK+ 2.12, not sure if it's still the same) that you could call
connect_signals
only once, any signals which are not connected during the first invocation will never be connected. This was different in glade, so be careful if you relied on that feature before.托斯顿的答案是正确的,但有点不完整,所以本着 http://xkcd.com/979/ 这是我最近经过多次尝试和错误后确定的程序:
在 Glade 界面设计器中打开 yada.glade。 转到编辑->项目并将项目类型更改为 GtkBuilder 并确保其目标是最新版本(截至撰写本文时为 2.24)。 保存文件,确保它以 GtkBuilder 格式保存,并将名称从 yada.glade 更改为 yada.ui
打开 yada.py 并将以下代码更改
为
:类似地更改
self.wTree.get_widget 的所有实例(...)
更改为self.wTree.get_object(...)
将
self.wTree.signal_autoconnect(dic)
更改为self.wTree .connect_signals(dic)
如果您的代码取决于界面设计器中为小部件指定的名称,请将
widget.get_name()
更改为gtk.Buildable.get_name(widget)< /代码>。
widget.get_name()
现在仅返回小部件类型。 编辑:您还需要将widget.set_name('my_widget')
更改为gtk.Buildable.set_name(widget, 'my_widget')
。删除
import gtk.glade
我发现 yada.ui xml 文件中定义了许多未使用的信号,我不得不打开 xml 文件并手动删除它们以消除它们引起的警告。
Torsten's answer is correct, but a little incomplete, so in the spirit of http://xkcd.com/979/ here is the procedure I recently settled on after much trial-and-error:
Open yada.glade in Glade interface designer. Go to edit->project and change the project type to GtkBuilder and make sure it targets the latest version (2.24 as of this writing). Save the file, being sure that it saves in GtkBuilder format, and change the name from yada.glade to yada.ui
Open yada.py and change the following code:
to:
Similarly change all instances of
self.wTree.get_widget(...)
toself.wTree.get_object(...)
Change
self.wTree.signal_autoconnect(dic)
toself.wTree.connect_signals(dic)
If your code depends on the name assigned the widget in the interface designer, change
widget.get_name()
togtk.Buildable.get_name(widget)
.widget.get_name()
now just returns the widget type. EDIT: You also need to changewidget.set_name('my_widget')
togtk.Buildable.set_name(widget, 'my_widget')
.Delete
import gtk.glade
I found numerous unused signals defined in the yada.ui xml file, I had to open the xml file and manually delete them to eliminate the warnings they caused.