Best practice with pyGTK and Builder XML files
Posted
by
Phoenix87
on Programmers
See other posts from Programmers
or by Phoenix87
Published on 2012-03-27T15:34:22Z
Indexed on
2012/03/27
17:42 UTC
Read the original article
Hit count: 330
python
I usually design GUI with Glade, thus producing a series of Builder XML files (one such file for each application window).
Now my idea is to define a class, e.g. MainWindow, that inherits from gtk.Window and that implements all the signal handlers for the application main window. The problem is that when I retrieve the main window from the containing XML file, it is returned as a gtk.Window instance.
The solution I have adopted so far is the following:
I have defined a class "Window" in the following way
class Window():
def __init__(self, win_name):
builder = gtk.Builder()
self.builder = builder
builder.add_from_file("%s.glade" % win_name)
self.window = builder.get_object(win_name)
builder.connect_signals(self)
def run(self):
return self.window.run()
def show_all(self):
return self.window.show_all()
def destroy(self):
return self.window.destroy()
def child(self, name):
return self.builder.get_object(name)
In the actual application code I have then defined a new class, say MainWindow, that inherits frow Window, and that looks like
class Main(Window):
def __init__(self):
Window.__init__(self, "main")
### Signal handlers #####################################################
def on_mnu_file_quit_activated(self, widget, data = None):
...
The string "main" refers to the main window, called "main", which resides into the XML Builder file "main.glade" (this is a sort of convention I decided to adopt).
So the question is: how can I inherit from gtk.Window directly, by defining, say, the class Foo(gtk.Window), and recast the return value of builder.get_object(win_name) to Foo?
© Programmers or respective owner