我在Glade 3.6.7中創建了一個簡單的窗口GUI,我試圖將它導入到Python中。每次我嘗試這樣做,我得到以下錯誤:Python Glade無法創建GladeXML對象
(queryrelevanceevaluation.py:8804): libglade-WARNING **: Expected <glade-interface>. Got <interface>.
(queryrelevanceevaluation.py:8804): libglade-WARNING **: did not finish in PARSER_FINISH state
Traceback (most recent call last):
File "queryrelevanceevaluation.py", line 17, in <module>
app = QueryRelevanceEvaluationApp()
File "queryrelevanceevaluation.py", line 10, in __init__
self.widgets = gtk.glade.XML(gladefile)
RuntimeError: could not create GladeXML object
我的Python代碼:
#!/usr/bin/env python
import gtk
import gtk.glade
class QueryRelevanceEvaluationApp:
def __init__(self):
gladefile = "foo.glade"
self.widgets = gtk.glade.XML(gladefile)
dic = {"on_buttonGenerate_clicked" : self.on_buttonGenerate_clicked}
self.widgets.signal_autoconnect(dic)
def on_buttonGenerate_clicked(self, widget):
print "You clicked the button"
app = QueryRelevanceEvaluationApp()
gtk.main()
而且foo.glade文件:
<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="windowRelevanceEvaluation">
<property name="visible">True</property>
<property name="title" translatable="yes">Query Result Relevance Evaluation</property>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkHBox" id="hbox2">
<property name="visible">True</property>
<child>
<object class="GtkLabel" id="labelQuery">
<property name="visible">True</property>
<property name="label" translatable="yes">Query:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="padding">4</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
</object>
<packing>
<property name="padding">4</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frameSource">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="left_padding">12</property>
<child>
<object class="GtkHButtonBox" id="hbuttonbox1">
<property name="visible">True</property>
<child>
<object class="GtkRadioButton" id="radiobuttonGoogle">
<property name="label" translatable="yes">Google</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="radiobuttonBing">
<property name="label" translatable="yes">Bing</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="radiobuttonBoden">
<property name="label" translatable="yes">Boden</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="radiobuttonCSV">
<property name="label" translatable="yes">CSV</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="labelFrameSource">
<property name="visible">True</property>
<property name="label" translatable="yes"><b>Source</b></property>
<property name="use_markup">True</property>
</object>
</child>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<child>
<object class="GtkHBox" id="hbox3">
<property name="visible">True</property>
<child>
<object class="GtkLabel" id="labelResults">
<property name="visible">True</property>
<property name="label" translatable="yes">Number Results:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="spinbuttonResults">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
</object>
<packing>
<property name="padding">4</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="labelFrameResults">
<property name="visible">True</property>
<property name="label" translatable="yes"><b>Results</b></property>
<property name="use_markup">True</property>
</object>
</child>
</object>
<packing>
<property name="padding">2</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="buttonGenerateResults">
<property name="label" translatable="yes">Generate!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
foo.glade和上面的python腳本都在同一個目錄下,並且我嘗試過使用完全限定的路徑,但仍然得到相同的錯誤(我確信路徑是正確的!)。
任何想法?
乾杯,
皮特
gladefile的外觀如何?這是絕對路徑嗎?你確定你的程序可以訪問它嗎? – joaquin 2010-04-19 15:29:50
我試過一個絕對路徑,只是「foo.glade」,發生了同樣的事情。在訪問文件時不應該有任何其他問題,它沒有特殊的文件限制,並且自從點擊Glade中的保存按鈕後我沒有觸及它。 – Peter 2010-04-19 15:34:41