putting glade interface in python

10,768

Solution 1

Try with this code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade

class HellowWorldGTK:

    def __init__(self):
        self.gladefile = "helloworld.glade" 
        self.glade = gtk.Builder()
        self.glade.add_from_file(self.gladefile)
        self.glade.connect_signals(self)
        self.glade.get_object("MainWindow").show_all()

    def on_MainWindow_delete_event(self, widget, event):
        gtk.main_quit()

if __name__ == "__main__":
    try:
        a = HellowWorldGTK()
        gtk.main()
    except KeyboardInterrupt:
        pass

Remember: In Glade, Edit the "Preferences" of the file to "GTKBuilder" (not "libglade")

Solution 2

Your PyHelloWorld.glade is incorrect. Make sure you created it with the correct Glade application, there are Glade2 and Glade3 applications that can be installed and used. If you downloaded the file make sure it is correct. The error message says it all:

Expected <glade-interface>.  Got  <interface>

So the XML file has the interface tag, but PyGTK library expects glade-interface tag.

Solution 3

Since I always end up having problems with this, here is a Python 2.7 code that I use for one or the other:

for Libglade:

# needs libglade (not for gtk-builder)
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade

gladefile = "test-libglade.glade"
wTree = gtk.glade.XML(gladefile)
window = wTree.get_widget("MainWindow")
if (window):
  window.connect("destroy", gtk.main_quit)

window.show_all() # must have!
gtk.main()

For GtkBuilder:

# needs gtk-builder (not for libglade)
import pygtk
pygtk.require("2.0")
import gtk

gladefile = "test-gtkbuilder.glade"
wTree = gtk.Builder()
wTree.add_from_file(gladefile)
window = wTree.get_object("MainWindow")
if (window):
  window.connect("destroy", gtk.main_quit)

window.show_all() # must have!
gtk.main()

In Glade, you can just add a Window, call it MainWindow, and save two versions with the respective filenames as above for each format; and these snippets should work with them respeactively.

Hope this helps someone,
Cheers!

Share:
10,768

Related videos on Youtube

Bianca
Author by

Bianca

Updated on June 04, 2022

Comments

  • Bianca
    Bianca almost 2 years

    I've made a gui in glade that I want to put in a python program. I was adapting the instructions from a tutorial I found online to load in my glade file (http://www.pygtk.org/articles/pygtk-glade-gui/Creating_a_GUI_using_PyGTK_and_Glade.htm). When I had problems I tried something basic (one button) calling it the same thing as in that tutorial, and copy pasting their code, and it still didn't work. I also took a look at (http://www.linuxjournal.com/article/6586?page=0,2), which has a function being called slightly differently ("self.wTree=gtk.glade.XML (gladefile,windowname)" instead of without windowname), and implemented an equivalent with mine and that didn't fix it. I definitely have pygtk working, I made something without using glade before and it worked fine. The error I'm getting is:

    /usr/share/themes/NOX/gtk-2.0/gtkrc:233: Murrine configuration option "gradients" 
     is no longer supported and will be ignored.
    
    (helloWorld.py:9804): libglade-WARNING **: Expected <glade-interface>.  Got    
     <interface>.
    
    (helloWorld.py:9804): libglade-WARNING **: did not finish in PARSER_FINISH state
    Traceback (most recent call last):
    File "helloWorld.py", line 31, in <module>
    hwg = HellowWorldGTK()
    File "helloWorld.py", line 22, in __init__
    self.wTree = gtk.glade.XML(self.gladefile) 
    RuntimeError: could not create GladeXML object
    

    I'm running xubuntu 11.04. The Murrine configuration thing comes up when any gtk application opens, but I included it in case it is relevant. Here's the code I took from the tutorial (but isn't working)

    #!/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 HellowWorldGTK:
    """This is an Hello World GTK application"""
    
    def __init__(self):
    
        #Set the Glade file
        self.gladefile = "PyHelloWorld.glade"  
        self.wTree = gtk.glade.XML(self.gladefile) 
    
        #Get the Main Window, and connect the "destroy" event
        self.window = self.wTree.get_widget("MainWindow")
        self.window.show()
        if (self.window):
            self.window.connect("destroy", gtk.main_quit)
    
    
    if __name__ == "__main__":
        hwg = HellowWorldGTK()
        gtk.main()
    
  • Bianca
    Bianca over 12 years
    Thanks, that's what I ended up doing (already, but it'll be good for other people's reference) :).
  • Tim Diekmann
    Tim Diekmann almost 6 years
    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Thanks for improving the answer's reference value and making it more understandable!