gui.glade:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<signal name="destroy" handler="on_window_destroy" swapped="no"/>
<child>
<object class="GtkLabel" id="label">
<property name="width_request">250</property>
<property name="height_request">100</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Hello, World!</property>
</object>
</child>
</object>
</interface>
gui.py:
#!/usr/bin/env python
import sys
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
import gtk.glade
except:
sys.exit(1)
class GTKWindow:
def __init__(self):
self.wTree = gtk.glade.XML("gui.glade")
self.window = self.wTree.get_widget("window")
if (self.window):
self.window.connect("on_window_destroy", gtk.main_quit)
gtk.main()
if __name__ == "__main__":
window = GTKWindow()
终端错误:
(gui.py:4504): libglade-WARNING **: Expected <glade-interface>. Got <interface>.
(gui.py:4504): libglade-WARNING **: did not finish in PARSER_FINISH state
Traceback (most recent call last):
File "gui.py", line 26, in <module>
window = GTKWindow()
File "gui.py", line 17, in __init__
self.wTree = gtk.glade.XML("gui.glade")
RuntimeError: could not create GladeXML object
我确保将 glade 文件保存在 Libglade 下,但是我无法让项目运行(请参阅上面终端中的错误)。我已经尝试使用 gtk.Builder 并且即使我将 glade 文件保存在 GtkBuilder 文件下也没有任何反应。有任何想法吗?