简体   繁体   English

Python GTK +小部件名称

[英]Python GTK+ widget name

How do I get a widget's "name"? 如何获取小部件的“名称”?

When I define a GUI using Glade, I can "name" the widgets of the window but how do I recover that property when I have a widget object instance? 当我使用Glade定义GUI时,我可以“命名”窗口的小部件,但是当我有小部件对象实例时如何恢复该属性?

I've tried get_property() , get_name() and $widget.name to no avail. 我试过get_property()get_name()$widget.name无济于事。

Update: I am using GtkBuilder file format (ie XML format). 更新:我使用的是GtkBuilder文件格式(即XML格式)。

Resolution: a fix I have used: use the set_property("name", name) method on the widget just after getting it from GtkBuilder . 解决方案:我使用过的修复:在从GtkBuilder获取后,在窗口小部件上使用set_property("name", name)方法。

I use the class-method form, which works for me: 我使用class-method表单,它对我有用:

gtk.Buildable.get_name(widget)

References 参考

PyGtk: gtk.Buildable.get_name PyGtk: gtk.Buildable.get_name

Gtk3: Gtk.Buildable.get_name Gtk3: Gtk.Buildable.get_name

Quote from gtk2 GtkBuilder : gtk2 GtkBuilder

Prior to GTK+ 2.20, gtk.Builder was setting the "name" property of constructed widgets to the "id" attribute. 在GTK + 2.20之前,gtk.Builder将构造小部件的“name”属性设置为“id”属性。 In GTK+ 2.20 or newer, you have to use gtk.Buildable.get_name() instead of gtk.Widget.get_name() to obtain the "id", or set the "name" property in your UI definition. 在GTK + 2.20或更高版本中,您必须使用gtk.Buildable.get_name()而不是gtk.Widget.get_name()来获取“id”,或在UI定义中设置“name”属性。


Further 进一步

There were some reported bugs where GtkBuilder sets the widget name to be the builder ID, or doesn't set it at all. 有一些报告的错误,GtkBuilder将窗口小部件名称设置为构建器ID,或者根本不设置它。 See Ubuntu and GNOME bugs. 查看UbuntuGNOME错误。 I have no idea why the bugs says that it sets the ID instead of the name as I've only ever seen the name not set. 我不知道为什么错误说它设置ID而不是名称,因为我只看到未设置的名称。

self.builder.get_name(widget)

Where widget is the widget object. 其中widget是widget对象。 Works using Ubuntu's packaged quickly development environment 使用Ubuntu打包的快速开发环境

Given the widget w , what does w.get_name() return? 给定小部件ww.get_name()返回什么? If None , that means the widget has no name property. 如果为None ,则表示窗口小部件没有name属性。 Maybe you want gtk.glade.get_widget_name(w) instead? 也许你想要gtk.glade.get_widget_name(w) (I'm not sure if the name property of the widget and the name for it in the Glade XML from which it was created are the same thing...). (我不确定窗口小部件的name属性及其创建的Glade XML中的名称是否相同......)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM