I am trying load a Gtk.Window, that I have defined with Glade, into an object that is derived from Gtk.Window. How can I do this?
Here is an example illustrating what I want to accomplish:
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
GLADE_FILE = 'my_ui.glade'
MAIN_WINDOW = 'main_window'
class MyInheritedWindow(Gtk.Window):
def __init__(self, something_special, **kwds):
super(MyInheritedWindow, self).__init__(**kwds)
self.my_word = something_special
#Load the user interface.
self.builder = Gtk.Builder.new_from_file(GLADE_FILE)
self.builder.connect_signals(self)
self.window = self.builder.get_object(MAIN_WINDOW)
self.builder.get_object('messagetext').set_text(self.my_word)
### WHAT DO I DO HERE TO MAKE THESE PRINT THE SAME OBJECT? ###
print(f'My window: {self}') #These are clearly
print(f'The Glade window: {self.window}') #different.
def on_destroy(self, widget, data=None):
Gtk.main_quit()
if __name__ == '__main__':
inherited_window = MyInheritedWindow('Please work?')
inherited_window.window.show_all()
Gtk.main()
inherited_window.show_all()
inherited_window.connect('destroy', Gtk.main_quit)
Gtk.main()
The first Gtk.main()
makes a window that looks like the Glade specification, but the window being shown is just an attribute of MyInheritedWindow
, and not an instance of it. The second Gtk.main()
makes a generic Gtk.Window
.
I would like to replace the Gtk.Window
part of the MyInheritedWindow
object with the Gtk.Window
created from the Glade files.
Notes:
I've found what I believe to be a similar question, but the accepted answer doesn't seem to do what I'm talking about.
This also may be the same question, but the answer is pretty convoluted. There must be a simpler way?
There also seems to be a way tell Glade about custom widgets but the article is from 2007.
For completeness, I'll add the glade file my_ui.glade
here:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="main_window">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Inheritance with Glade</property>
<signal name="destroy" handler="on_destroy" swapped="no"/>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">Inheritance Example</property>
<property name="subtitle" translatable="yes">(will it work?)</property>
<property name="show_close_button">True</property>
</object>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="messagetext">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Replace this text.</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>