Python Glade could not create GladeXML Object

痞子三分冷 提交于 2019-12-18 12:57:15

问题


I've created a simple window GUI in Glade 3.6.7 and I am trying to import it into Python. Every time I try to do so I get the following error:

(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

My Python Code:

#!/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()

And the foo.glade file:

<?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 and the above python script are in the same directory, and I have tried using a fully-qualified path but still get the same error (I am certain that the path is correct!).

Any ideas?

Cheers,

Pete


回答1:


You have created a GtkBuilder file instead of Glade file.

You can use GtkBuilder as follow:

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

EDIT:

When you start a new project in glade it asks you if you want create a glade file or a GtkBuilder file, which is new and more flexible. Try the builder file with the following code:

#!/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:

Beware that i cannot see any handler in your GtkBuilder file




回答2:


I had the same problem Pete mentioned. After the answer that mg gave, what I did is to save the .glade file in a liblgade format instead of GTKBuilder. On the Save As.. dialog box, at the bottom you have the option "File Format" click on "liblglade" and voila!!!!



来源:https://stackoverflow.com/questions/2668618/python-glade-could-not-create-gladexml-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!