从使用 libglade 转换为 GtkBuilder 有哪些步骤? (Python)

发布于 2024-07-16 22:46:18 字数 460 浏览 5 评论 0原文

我有一个使用 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 技术交流群。

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

发布评论

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

评论(2

您需要使用 gtk.Builder 来代替。 此类可以加载任意数量的 UI 文件,因此您需要以文件或字符串的形式手动添加它们:

self.uifile = "sdm.ui"
self.wTree = gtk.Builder()
self.wTree.add_from_file(self.uifile)

只需在构建器类上使用 get_object 即可代替 get_widget

self.window = self.wTree.get_object("MainWindow")
if self.window:
    self.window.connect("destroy", gtk.main_quit)

要连接信号,只需使用 connect_signals ,它也需要一个字典:

dic = { "on_button1_clicked" : self.button1_clicked, 
    "on_MainWindow_destroy" : gtk.main_quit}
self.wTree.connect_signals(dic)

过去的情况(至少在 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:

self.uifile = "sdm.ui"
self.wTree = gtk.Builder()
self.wTree.add_from_file(self.uifile)

Instead of get_widget, just use get_object on the builder class:

self.window = self.wTree.get_object("MainWindow")
if self.window:
    self.window.connect("destroy", gtk.main_quit)

To connect the signals, just use connect_signals, which also takes a dictionary:

dic = { "on_button1_clicked" : self.button1_clicked, 
    "on_MainWindow_destroy" : gtk.main_quit}
self.wTree.connect_signals(dic)

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.

心不设防 2024-07-23 22:46:18

托斯顿的答案是正确的,但有点不完整,所以本着 http://xkcd.com/979/ 这是我最近经过多次尝试和错误后确定的程序:

在 Glade 界面设计器中打开 yada.glade。 转到编辑->项目并将项目类型更改为 GtkBuilder 并确保其目标是最新版本(截至撰写本文时为 2.24)。 保存文件,确保它以 GtkBuilder 格式保存,并将名称从 yada.glade 更改为 yada.ui

打开 yada.py 并将以下代码更改

gladefile = relativize_filename(os.path.join("glade", "yada.glade"))
self.wTree = gtk.glade.XML(gladefile, self.windowname)

uifile = relativize_filename(os.path.join("glade", "yada.ui"))
self.wTree = gtk.Builder()
self.wTree.add_from_file(uifile)

:类似地更改 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:

gladefile = relativize_filename(os.path.join("glade", "yada.glade"))
self.wTree = gtk.glade.XML(gladefile, self.windowname)

to:

uifile = relativize_filename(os.path.join("glade", "yada.ui"))
self.wTree = gtk.Builder()
self.wTree.add_from_file(uifile)

Similarly change all instances of self.wTree.get_widget(...) to self.wTree.get_object(...)

Change self.wTree.signal_autoconnect(dic) to self.wTree.connect_signals(dic)

If your code depends on the name assigned the widget in the interface designer, change widget.get_name() to gtk.Buildable.get_name(widget). widget.get_name() now just returns the widget type. EDIT: You also need to change widget.set_name('my_widget') to gtk.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.

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