Python Glade无法创建GladeXML对象

时间:2010-04-19 15:24:56

标签: python user-interface exception glade

我在Glade 3.6.7中创建了一个简单的窗口GUI,我试图将其导入Python。每次我尝试这样做时都会出现以下错误:

(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">&#x25CF;</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">&lt;b&gt;Source&lt;/b&gt;</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">&#x25CF;</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">&lt;b&gt;Results&lt;/b&gt;</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脚本在同一目录中,我尝试使用完全限定的路径,但仍然得到相同的错误(我确定路径是正确的!)。

有什么想法吗?

干杯,

皮特

2 个答案:

答案 0 :(得分:23)

您已经创建了一个GtkBuilder文件,而不是Glade文件。

您可以按照以下方式使用GtkBuilder:

builder = gtk.Builder()
builder.add_from_string(string, len(string))
builder.connect_signals(anobject)
builder.get_object(name)

修改

当您在林间开始一个新项目时,它会询问您是否要创建一个新的,更灵活的林间文件或GtkBuilder文件。 使用以下代码尝试构建器文件:

#!/usr/bin/env python

import gtk

class  QueryRelevanceEvaluationApp:

    def __init__(self):
        filename = "foo.glade"
        builder = gtk.Builder()
        builder.add_from_file(filename)
        builder.connect_signals(self)

    def on_buttonGenerate_clicked(self, widget):
        print "You clicked the button"

app = QueryRelevanceEvaluationApp()
gtk.main()

<强> EDIT2:

请注意,我在GtkBuilder文件中看不到任何处理程序

答案 1 :(得分:5)

我有同样的问题,皮特提到过。在mg给出的答案之后,我所做的是以liblgade格式而不是GTKBuilder保存.glade文件。在“另存为...”对话框的底部,您可以选择“文件格式”,单击“liblglade”并瞧!!!!