putting glade interface in python

百般思念 提交于 2019-12-03 06:27:55
MasterSieben

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")

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.

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!

Ernestas Žaglinskas

This works perfectly.

#!/usr/bin/python
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
class SubinsWindow:
 def __init__(self):
  self.gladefile = "game.glade"
  self.glade = gtk.Builder()
  self.glade.add_from_file(self.gladefile)
  self.glade.connect_signals(self)
  self.win=self.glade.get_object("window1") # Window Name in GLADE
  self.win.show_all()

if __name__ == "__main__":
 a = SubinsWindow()
 gtk.main()

If you are using GTK+3 in python, see builder.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class Handler:
    def onDestroy(self, *args):
        Gtk.main_quit()

    def onButtonPressed(self, button):
        print("Hello World!")

builder = Gtk.Builder()
builder.add_from_file("builder_example.glade")
builder.connect_signals(Handler())

window = builder.get_object("window1")
window.show_all()

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