[英]Using Gtk.Window defined in Glade for a class derived from Gtk.Window with Python GTK
I am trying load a Gtk.Window, that I have defined with Glade, into an object that is derived from Gtk.Window. 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.第一个
Gtk.main()
使 window 看起来像 Glade 规范,但显示的 window 只是MyInheritedWindow
的一个属性,而不是它的一个实例。 The second Gtk.main()
makes a generic Gtk.Window
.第二个
Gtk.main()
生成通用Gtk.Window
。
I would like to replace the Gtk.Window
part of the MyInheritedWindow
object with the Gtk.Window
created from the Glade files. 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.似乎还有一种方法可以告诉 Glade 自定义小部件,但这篇文章来自 2007 年。
For completeness, I'll add the glade file my_ui.glade
here:为了完整起见,我将在此处添加林间空地文件
my_ui.glade
:
<?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>
This is the best I have come up with.这是我想出的最好的。 I read the Gtk.Window using the Builder and then I add all the child widgets to my window (after removing them from the other window.
我使用 Builder 阅读了 Gtk.Window 然后我将所有子小部件添加到我的 window (从其他 window 中删除它们之后。
#!/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 MyWindow(Gtk.Window):
def __init__(self, something_special, **kwds):
super(MyWindow, 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)
window = self.builder.get_object(MAIN_WINDOW)
self.builder.get_object('messagetext').set_text(self.my_word)
self.messagetext = self.builder.get_object('messagetext')
self.copy_window_children(window)
def copy_window_children(self, window: Gtk.Window):
""" Copy the window Header and other widgets """
for child in window.get_children():
window.remove(child)
if type(child) == Gtk.HeaderBar:
self.set_titlebar(child)
else:
self.add(child)
if __name__ == '__main__':
mywindow = MyWindow('It worked.')
mywindow.show_all()
mywindow.connect('destroy', Gtk.main_quit)
Gtk.main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.