在Glade中暴露GTK3自定义小部件属性

huangapple go评论52阅读模式
英文:

Exposing GTK3 custom widget properties in glade

问题

I can add a custom widget to GTK, as shown here. Now I'd like to add properties to the custom widget and have them show up in glade. Is this possible?

我可以像这里所示添加自定义小部件到GTK。现在,我想为自定义小部件添加属性,并让它们显示在glade中。这是否可能?

I have added properties to the custom widget as shown here but the properties do not show up in glade.

这里所示,我已经为自定义小部件添加了属性,但属性不会显示在glade中。

Update

It appears that the glade catalogue supports a <parameter-spec> element that can be used to define properties to be exposed within glade as shown below.

看起来glade目录支持<parameter-spec>元素,可以用来定义在glade中公开的属性,如下所示:

<glade-widget-class title="Awesome TextView" name="AwesomeTextView" generic-name="awesome_text_view">
    <properties>
        <property id="num_fidgets" name="Number of fidgets" default="4" save="False" custom-layout="True">
            <parameter-spec>
                <type>GParamInt</type>
                <min>4</min>
            </parameter-spec>
            <tooltip>The number of fidgets</tooltip>
        </property>
    </properties>
</glade-widget-class>

I have defined one such property but it does not show in glade. Rather I get the error message:

我定义了一个这样的属性,但它在glade中不显示。相反,我收到了错误消息:

GLib-GObject-CRITICAL **: 16:07:45.433: g_object_set_is_valid_property: object class 'AwesomeTextView' has no property named 'num-fidgets'

Where the custom widget AwesomeTextView is essentially defined as:

自定义小部件AwesomeTextView基本上被定义为:

class AwesomeTextView (Gtk.TextView):
    __gtype_name__ = 'AwesomeTextView'
    
    num_fidgets = GObject.Property(type=int, default='4', nick='num_Fidgets')

The <parameter-spec>s appear to be undocumented. Thankfully though there are examples of the supported <type>s in the glade repo.

似乎没有文档记录<parameter-spec>。不过,幸运的是,在glade的仓库中有支持的<type>的示例。

Correction: <parameter-spec>s are documented. This documentation can be supplemented with examples of the supported <type>s in the glade repo.

更正<parameter-spec>已经有文档记录。可以通过在glade的仓库中提供支持的<type>示例来补充这份文档。

Update2

Found the cause of the error. The default argument has the wrong type -- str instead of int. The property should be:

找到错误的原因。默认参数的类型错误 - 应为int而不是str。属性应该如下:

num_fidgets = GObject.Property(type=int, default=4, nick='num_Fidgets')

Glade no longer emits an error. But the property does not show up in the UI.

Glade不再报错。但是属性仍然不会显示在用户界面中。

英文:

I can add a custom widget to GTK, as shown here. Now I'd like to add properties to the custom widget and have them show up in glade. Is this possible?

I have added properties to the custom widget as shown here but the properties do not show up in glade.


Update

It appears that the glade catalogue supports a &lt;parameter-spec&gt; element that can be used to define properties to be exposed within glade as shown below.

&lt;glade-widget-class title=&quot;Awesome TextView&quot; name=&quot;AwesomeTextView&quot; generic-name=&quot;awesome_text_view&quot;&gt;
	&lt;properties&gt;
		&lt;property id=&quot;num_fidgets&quot; name=&quot;Number of fidgets&quot; default=&quot;4&quot; save=&quot;False&quot; custom-layout=&quot;True&quot;&gt;
		&lt;parameter-spec&gt;
			&lt;type&gt;GParamInt&lt;/type&gt;
			&lt;min&gt;4&lt;/min&gt;
		&lt;/parameter-spec&gt;
		&lt;tooltip&gt;The number of fidgets&lt;/tooltip&gt;
		&lt;/property&gt;
	&lt;/properties&gt;
&lt;/glade-widget-class&gt;

I have defined one such property but it does not show in glade. Rather I get the error message:

GLib-GObject-CRITICAL **: 16:07:45.433: g_object_set_is_valid_property: object class &#39;AwesomeTextView&#39; has no property named &#39;num-fidgets&#39;

Where the custom widget AwesomeTextView is essentially defined as:

class AwesomeTextView (Gtk.TextView):
	__gtype_name__ = &#39;AwesomeTextView&#39;
	
	num_fidgets = GObject.Property(type=int, default=&#39;4&#39;, nick=&#39;num_Fidgets&#39;)

<s>The &lt;parameter-spec&gt;s appear to be undocumented. Thankfully though there are examples of the the supported &lt;type&gt;s in the glade repo.</s>

Correction: &lt;parameter-spec&gt;s are documented. This documentation can be supplemented with examples of the supported &lt;type&gt;s in the glade repo.


Update2

Found the cause of the error. The default argument has the wrong type -- str instead of int. The property should be:

num_fidgets = GObject.Property(type=int, default=4, nick=&#39;num_Fidgets&#39;)

Glade no longer emits an error. But the property does not show up in the UI.

答案1

得分: 0

以下是翻译好的部分:

过于复杂的解决方案(请查看简化解决方案的更新)

找到了最后一部分。不要将&lt;property&gt;属性的custom-layout设置为&quot;True&quot;,除非你知道自己在做什么,也就是说,你知道如何正确布局该属性(至少我不知道,至少目前还不知道)。我建议,在下面的更新的目录中将custom-layout=&quot;False&quot;设置为:

<glade-catalog name=&quot;awesome_text_view&quot; library=&quot;gladepython&quot; domain=&quot;glade-3&quot; depends=&quot;gtk+&quot;>
    <init-function&gt;glade_python_init&lt;/init-function&gt;

    <glade-widget-classes&gt;
        <glade-widget-class title=&quot;Awesome TextView&quot; name=&quot;AwesomeTextView&quot; generic-name=&quot;awesome_text_view&quot;&gt;
            <properties&gt;
                <property id=&quot;num_fidgets&quot; name=&quot;Number of fidgets&quot; default=&quot;4&quot; save=&quot;True&quot; custom-layout=&quot;False&quot;&gt;
                    <parameter-spec&gt;
                        <type&gt;GParamInt&lt;/type&gt;
                        <min&gt;4&lt;/min&gt;
                    </parameter-spec&gt;
                    <tooltip&gt;The number of fidgets&lt;/tooltip&gt;
                </property&gt;
            </properties&gt;
        </glade-widget-class&gt;
    </glade-widget-classes&gt;

    <glade-widget-group name=&quot;Python&quot; title=&quot;Python&quot;&gt;
        <glade-widget-class-ref name=&quot;AwesomeTextView&quot;/&gt;
    </glade-widget-group&gt;
</glade-catalog&gt;

结果:

在Glade中暴露GTK3自定义小部件属性

为了完整起见,这是示例自定义小部件类:

import gi
gi.require_version(&quot;Gtk&quot;, &quot;3.0&quot;)
from gi.repository import GObject
from gi.repository import Gtk

class AwesomeTextView(Gtk.TextView):
    __gtype_name__ = &#39;AwesomeTextView&#39;
    num_fidgets = GObject.Property(type=int, default=4, nick=&#39;num_fidgets&#39;)

    def __init__(self):
        Gtk.TextView.__init__(self)

更新

原来在属性声明中的nick参数

num_fidgets = GObject.Property(type=int, default=4, nick=&#39;num_fidgets&#39;)

是属性出现在Glade用户界面中所必需的。元素&lt;properties&gt;更适合更复杂的值,例如枚举。我将保留更复杂的答案,以防对任何人有用。

英文:

Overly complicated solution (See update for simpler solution)

Found the final piece. Do not set the &lt;property&gt; attribute custom-layout to &quot;True&quot; -- unless you know what are doing i.e. you know how to layout the property, properly (I don't, at least not yet). My advise, set custom-layout=&quot;False&quot; in the updated catalogue shown below:

&lt;glade-catalog name=&quot;awesome_text_view&quot; library=&quot;gladepython&quot; domain=&quot;glade-3&quot; depends=&quot;gtk+&quot;&gt;
	&lt;init-function&gt;glade_python_init&lt;/init-function&gt;

	&lt;glade-widget-classes&gt;
		&lt;glade-widget-class title=&quot;Awesome TextView&quot; name=&quot;AwesomeTextView&quot; generic-name=&quot;awesome_text_view&quot;&gt;
			&lt;properties&gt;
				&lt;property id=&quot;num_fidgets&quot; name=&quot;Number of fidgets&quot; default=&quot;4&quot; save=&quot;True&quot; custom-layout=&quot;False&quot;&gt;
					&lt;parameter-spec&gt;
						&lt;type&gt;GParamInt&lt;/type&gt;
						&lt;min&gt;4&lt;/min&gt;
					&lt;/parameter-spec&gt;
					&lt;tooltip&gt;The number of fidgets&lt;/tooltip&gt;
				&lt;/property&gt;
			&lt;/properties&gt;
		&lt;/glade-widget-class&gt;
	&lt;/glade-widget-classes&gt;

	&lt;glade-widget-group name=&quot;Python&quot; title=&quot;Python&quot;&gt;
		&lt;glade-widget-class-ref name=&quot;AwesomeTextView&quot;/&gt;
	&lt;/glade-widget-group&gt;
&lt;/glade-catalog&gt;

Result:

在Glade中暴露GTK3自定义小部件属性

For completeness here is the sample custom widget class

import gi
gi.require_version(&quot;Gtk&quot;, &quot;3.0&quot;)
from gi.repository import GObject
from gi.repository import Gtk

class AwesomeTextView(Gtk.TextView):
	__gtype_name__ = &#39;AwesomeTextView&#39;
	num_fidgets = GObject.Property(type=int, default=4, nick=&#39;num_fidgets&#39;)

	def __init__(self):
		Gtk.TextView.__init__(self)

Update

Turns out the nick argument in the property declaration

num_fidgets = GObject.Property(type=int, default=4, nick=&#39;num_fidgets&#39;)

is all that's required to for the property to show up in the glade UI. The element &lt;properties&gt; would be more appropriate for more complicated values e.g. enums. I'm leaving the more complicated answer in place in the event that its of use to anyone.

huangapple
  • 本文由 发表于 2023年6月13日 01:36:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定