I have made my whole GUI definition over glade, I'm using PyGObject with python 2.7. I have made some widgets that have ids and I can retrieve those objects by calling the corresponding id, for now I have been doing something like this:
class MLPNotebookTab:
def __init__(self):
builder = Gtk.Builder.new_from_file(UI_FILE)
builder.connect_signals(self)
self.notebook = builder.get_object('MLPNotebook')
def add_tab(self, content):
pages = self.notebook.get_n_pages()
label = "MLP #" + str(pages + 1)
tab = NotebookTabLabel(label, self.notebook, pages + 1)
self.notebook.append_page(content, tab.header)
def on_btn_add_tab_clicked(self, button):
self.add_tab(Gtk.Label(label= "test"))
The ui file definition is just the same as it would be, it's just a notebook. Want I want is to make the class a notebook itself and preload the other attributes that we set in the ui file. I have found some kind of implementation in here: https://eeperry.wordpress.com/2013/01/05/pygtk-new-style-python-class-using-builder/
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gtk, sys, os
class MainWindow(gtk.Window):
__gtype_name__ = "MainWindow"
def __new__(cls):
"""This method creates and binds the builder window to class.
In order for this to work correctly, the class of the main
window in the Glade UI file must be the same as the name of
this class."""
app_path = os.path.dirname(__file__)
try:
builder = gtk.Builder()
builder.add_from_file(os.path.join(app_path, "main.ui"))
except:
print "Failed to load XML GUI file main.ui"
sys.exit(1)
new_object = builder.get_object('window')
new_object.finish_initializing(builder)
return new_object
def finish_initializing(self, builder):
"""Treat this as the __init__() method.
Arguments pass in must be passed from __new__()."""
builder.connect_signals(self)
# Add any other initialization here
I don't know if that's the best way to do it. Please help!