如何更改GTK窗口小部件的属性,如堆栈? [英] How do I change the property of a GTK widget such as a stack?

查看:112
本文介绍了如何更改GTK窗口小部件的属性,如堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在快速浏览GtkStack的GTK 3.10文档。
$ b



它提到添加到GtkStack的孩子获得了一个属性icon-name。



我的问题是 - 如何更改此属性的值。原因 - 我想将GtkStackSwitcher按钮更改为图标而不是文本,但我想对此编码 - 不使用GktBuilder用户界面。



如果您查看 gtk3-demo - 堆栈演示和UI文件,您可以看到GtkSwitcher有一个图标。

https://i.stack.imgur.com/6eJLa.pngalt =在这里输入图片描述>

  <?xml version =1.0encoding =UTF-8?> 
< interface>
<! - interface-requires gtk + 3.6 - >>
< object class =GtkWindowid =window1>
< property name =can_focus> False< / property>
< property name =titletranslatable =yes> GtkStack< / property>
< child>
< object class =GtkGridid =grid1>
< property name =visible> True< / property>
< property name =can_focus> False< / property>
< child>
< object class =GtkStackSwitcherid =switcher>
< property name =visible> True< / property>
< property name =stack> stack< / property>
< property name =halign>中心< / property>
< / object>
<包装>
< 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 =GtkStackid =stack>
< property name =visible> True< / property>
< property name =can_focus> True< / property>
< property name =transition-type>交叉淡入淡出< / property>
< child>
< object class =GtkImageid =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>
<包装>
< property name =name> page1< / property>
< / packing>
< / child>
< child>
< object class =GtkCheckButtonid =checkbutton1>
< property name =labeltranslatable =yes> Page 2< / property>
< property name =visible> True< / property>
< property name =can_focus> True< / property>
< property name =receiving_default> False< / property>
< property name =xalign> 0< / property>
< property name =draw_indicator> True< / property>
< property name =halign>中心< / property>
< property name =valign>中心< / property>
< / object>
<包装>
< property name =name> page2< / property>
< / packing>
< / child>
< child>
< object class =GtkSpinnerid =spinner1>
< property name =visible> True< / property>
< property name =can_focus> False< / property>
< property name =halign>中心< / property>
< property name =valign>中心< / property>
< property name =active> True< / property>
< / object>
<包装>
< property name =name> page3< / property>
< property name =icon-name> face-laugh-symbolic< / property>
< / packing>
< / child>
< / object>
<包装>
< 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(默认,2014年4月11日,13:05:11)
[GCC 4.8.2]在linux
上输入help ,版权,信用或许可证以获取更多信息。
>>> from gi.repository import Gtk
>>> stack = Gtk.Stack()
>>> spin = Gtk.Spinner()
>>> stack.add_named(旋转,旋转)
>>> stack.show_all()
>>> child = stack.get_visible_child()
>>> child.props.icon_name =go-previous
Traceback(最近一次调用的最后一个):
在< module>中的第1行文件< stdin>
AttributeError:'gi._gobject.GProps'对象没有属性'icon_name'
>>> stack.props.icon_name =go-previous
Traceback(最近一次调用最后一次):
在< module>中,第1行的文件< stdin>
AttributeError:'gi._gobject.GProps'对象没有属性'icon_name'
>>>

正如你所看到的 - 我已经添加了一个GtkSpinner到一个堆栈对象 - 我尝试过访问已添加的孩子,然后更改icon-name属性。



没有快乐。那么我所做的明显的错误是什么?

解决方案

轻微但重要的区别:名称不是 GtkStack 子元素的属性,它是 GtkStack



不同之处在于,子属性影响容器与子容器之间的关系,而不仅仅是容器或

使用这个:

  stack.child_set_property (孩子,'icon-name','go-previous')


I've been taking a quick look at the GTK 3.10 documentation for a GtkStack.

It mentions that the child added to a GtkStack is given a property "icon-name".

My question is - how can I change the value of this property. The reason - I want to change the GtkStackSwitcher button to be an icon not text but I want to code this - not use a GktBuilder UI.

If you look at the gtk3-demo - Stack demo and the UI file you can see the GtkSwitcher has an icon.

<?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>

Using the python3 interpreter under Ubuntu 14.04 (Gtk 3.10)

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'
>>> 

As you can see - I've added a GtkSpinner to a stack object - and I've attempted to access the child that was added and then change the property "icon-name".

No joy. So what is the obvious mistake I'm making?

解决方案

Slight but important distinction: the icon-name isn't a "property" of the child of a GtkStack, it's a "child property" of GtkStack.

The difference is that a "child property" affects the relation between a container and one of its child widgets, rather than just the container or just the child widget.

Use this:

stack.child_set_property(child, 'icon-name', 'go-previous')

这篇关于如何更改GTK窗口小部件的属性,如堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆