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文件下也没有任何反应。有什么想法吗?
答案 0 :(得分:0)
我在代码中做了一些更改。 我希望能帮到你。
<强> gui.glade 强>
<?xml version="1.0" encoding="UTF-8"?> <glade-interface> <!--
interface-requires gtk+ 2.24 --> <!-- interface-naming-policy
project-wide --> <widget class="GtkWindow" id="window">
<property name="visible">True</property>
<property name="can_focus">False</property>
<signal name="destroy" handler="on_window_destroy" after="yes" swapped="no"/>
<child>
<widget class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Hello, World!</property>
</widget>
</child> </widget> </glade-interface>
<强> gui.py 强>
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")
dict = {
"on_window1_destroy": self.destroy,
}
self.wTree.signal_autoconnect(dict)
def destroy(self,*args):
gtk.main_quit()
# def
if __name__ == "__main__":
window = GTKWindow()
gtk.main()