Python Glade could not create GladeXML Object
Asked Answered
T

2

14

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

Trap answered 19/4, 2010 at 15:24 Comment(2)
how gladefile exactly looks like? Is it an absolute path? Are you sure your program can access it?Harebrained
I've tried an absolute path and just "foo.glade" and the same thing happened. There shouldn't be any other problem in accessing the file, it has no special file restrictions on it and I haven't touched it since hitting the save button in Glade.Trap
J
23

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

Jarmon answered 19/4, 2010 at 16:8 Comment(4)
What would I have to create in order to use the code above and how is this achieved using Glade? (Just for completeness) :)Trap
Peter: GtkBuilder is the preferred method, using libglade is deprecated, so, don't. :-)Tindle
Useful code snippet, but it missed an important step: builder.get_object("windowRelevanceEvaluation").show_all()Hearst
@RobbieMatthews Where in the code would that piece of code go? Immediately after builder.connect_signals(self) ? That's sort of what is written in the first code sample, but I want to make sure.Mudra
P
5

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!!!!

Precondemn answered 22/6, 2010 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.