How I can create a python class that takes it definition from Gtk builder

ぃ、小莉子 提交于 2021-02-08 08:11:44

问题


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!


回答1:


You can use this third party library (just copy into tree): https://github.com/virtuald/pygi-composite-templates

And it would look like this:

from gi.repository import Gtk
from gi_composites import GtkTemplate

PATH_TO_UI_FILE='foo'

@GtkTemplate(ui=PATH_TO_UI_FILE)
class MLPNotebook(Gtk.Notebook):
    __gtype_name__ = 'MLPNotebook'

    def __init__(self):
        Gtk.Notebook.__init__(self)
        self.init_template()

With your UI file containing a template widget:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <template class="MLPNotebook" parent="GtkNotebook">
    <!-- Stuff goes here -->
  </template>
</interface>

Note the fact its a notebook was just a random example based upon your widget name that probably doesn't make sense vs it being another widget type.



来源:https://stackoverflow.com/questions/45539787/how-i-can-create-a-python-class-that-takes-it-definition-from-gtk-builder

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