我已经按照以下规范为我的GTK项目创建了Glade UI:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<template class="Pages" parent="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="page_title">
<property name="visible">True</property>
<property name="label" translatable="yes">Title</property>
<attributes>
<attribute name="weight" value="normal"/>
<attribute name="scale" value="1.5"/>
</attributes>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
</template>
</interface>
此Glade UI链接到另一个在首次启动应用程序时默认运行的UI。
在提到here的API之后,我尝试在python实现中引用标签page_title
:
from gi.repository import Gtk
@Gtk.Template(resource_path='/com/bscubed/App/ui/pages.ui')
class Pages(Gtk.Box):
__gtype_name__ = 'Pages'
page_title = Gtk.Template.Child()
def __init__(self, **kwargs):
super().__init__(**kwargs)
print(type(self.page_title))
self.page_title.set_text("Test")
但是输出始终是
<class 'gi._gtktemplate.Child'>
AttributeError: 'Child' object has no attribute 'set_text'
而且我无法访问标签通常可用的任何方法。
但是,模板定义在主窗口实现中很好用:
from gi.repository import Gtk
from .pages import Pages
@Gtk.Template(resource_path='/com/bscubed/App/ui/window.ui')
class MainWindow(Gtk.ApplicationWindow):
__gtype_name__ = 'MainWindow'
toolbar_stack = Gtk.Template.Child()
content_stack = Gtk.Template.Child()
start_button = Gtk.Template.Child()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.start_button.connect("clicked", self.next_page)
print(type(self.start_button))
def next_page(self, button):
self.content_stack.set_visible_child_name("pages")
self.toolbar_stack.set_visible_child_name("pages_toolbar")
其输出为<class 'gi.overrides.Gtk.Button'>
所以PyGObject发生了非常奇怪的事情。难道我做错了什么?感谢您的任何帮助,并在此先感谢您。
gi_composites.py
目录中第162行的/your_project/src
文档