python – 如何更改堆栈等GTK小部件的属性?
作者:互联网
我一直在快速浏览一下GtkStack的GTK 3.10文档.
> https://developer.gnome.org/gtk3/3.10/GtkStack.html
它提到添加到GtkStack的子项被赋予属性“icon-name”.
我的问题是 – 如何更改此属性的值.原因 – 我想将GtkStackSwitcher按钮更改为图标而不是文本,但我想对此进行编码 – 而不是使用GktBuilder UI.
如果您查看gtk3-demo – Stack演示和UI文件,您可以看到GtkSwitcher有一个图标.
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.6 -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="title" translatable="yes">GtkStack</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkStackSwitcher" id="switcher">
<property name="visible">True</property>
<property name="stack">stack</property>
<property name="halign">center</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkStack" id="stack">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="transition-type">crossfade</property>
<child>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin-top">20</property>
<property name="margin-bottom">20</property>
<property name="resource">/application/gtk-logo-48.png</property>
</object>
<packing>
<property name="name">page1</property>
<property name="title" translatable="yes">Page 1</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="checkbutton1">
<property name="label" translatable="yes">Page 2</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
</object>
<packing>
<property name="name">page2</property>
<property name="title" translatable="yes">Page 2</property>
</packing>
</child>
<child>
<object class="GtkSpinner" id="spinner1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="active">True</property>
</object>
<packing>
<property name="name">page3</property>
<property name="icon-name">face-laugh-symbolic</property>
</packing>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
在Ubuntu 14.04(Gtk 3.10)下使用python3解释器
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import Gtk
>>> stack = Gtk.Stack()
>>> spin = Gtk.Spinner()
>>> stack.add_named(spin, "spin")
>>> stack.show_all()
>>> child = stack.get_visible_child()
>>> child.props.icon_name = "go-previous"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'gi._gobject.GProps' object has no attribute 'icon_name'
>>> stack.props.icon_name = "go-previous"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'gi._gobject.GProps' object has no attribute 'icon_name'
>>>
正如您所看到的 – 我已经将GtkSpinner添加到堆栈对象中 – 我试图访问已添加的子项,然后更改属性“icon-name”.
没有快乐.那么我正在做的那个明显的错误是什么?
解决方法:
稍微但重要的区别:图标名称不是GtkStack子项的“属性”,它是GtkStack的“子属性”.
不同之处在于“子属性”会影响容器与其中一个子窗口小部件之间的关系,而不仅仅是容器或子窗口小部件.
用这个:
stack.child_set_property(child, 'icon-name', 'go-previous')
标签:python,gtk,gtk3,pygobject 来源: https://codeday.me/bug/20190722/1506847.html