3

我在 GtkEventBox 中有一个 GtkImage,在 glade 上定义

 <object class="GtkEventBox" id="eb_paciente">
    <signal name="button-press-event" handler="print_filepath" object="bt_icn_paciente" swapped="no"/>
    <child>
      <object class="GtkImage" id="bt_icn_paciente">
       <property name="pixbuf">img/patient_icons/icon_patient.png</property>
      </object>
    </child>
  </object>

我要做的是在单击 EventBox 时更改图像路径。

我在创建硬编码的事件框之前就这样做了,并且工作起来就像一个魅力。但现在我无法获得“文件”属性。我已经尝试了两种方法,使用g_object_getand g_object_get_property,但我总是得到一个 NULL 对象作为响应,而不是文件名。

这是我的代码

gboolean print_filepath(GtkWidget *widget, GdkEvent  *event, gpointer *data){
  GValue value = G_VALUE_INIT;

  g_value_init (&value, G_TYPE_STRING);
  g_object_get_property(G_OBJECT(gtk_bin_get_child(GTK_EVENT_BOX(widget))), "file", &value);
  g_print("\n print_filepath = %s\n",  g_value_get_string(&value));

  gchar *filepath;
  g_object_get(G_OBJECT(data), "file", &filepath, NULL);
  g_print("\n print_filepath = %s\n", filepath);
  g_free(filepath);

  return FALSE;
}

我在互联网上进行了深入搜索,但一无所获。我试图找出另一种解决方案来实现我想要的,但我不知道该怎么做。我将不胜感激任何帮助或建议。单击事件框时如何更改图像文件?非常感谢!

4

2 回答 2

1

关键是你或者更确切地说GtkBuilder不初始化file你的GtkImage对象的属性。所以你得到了预期的默认值,fileNULL

相反,默认情况下glade似乎将pixbuf属性设置为文件路径。这是完全有效的,并记录在GtkBuilder UI Definitions参考部分:

Pixbufs 可以指定为要加载的图像文件的文件名

我换了行

<property name="pixbuf">img/patient_icons/icon_patient.png</property>

您的 ui 定义文件到

<property name="file">img/patient_icons/icon_patient.png</property>

它在我的系统上按最初预期工作。

于 2013-07-29T12:08:43.000 回答
0

来自 user1146332 的回答更改线路

<property name="pixbuf">img/patient_icons/icon_patient.png</property>

<property name="file">img/patient_icons/icon_patient.png</property>

将允许您获取文件名。谢谢你。

在 python 中,你会得到这样的文件名:

ImageFileName = widget.get_property('file')

如果您计划在 glade 上进行设计并修改您的 *.glade 文件。您将不得不每次修改它。这是一些“修复”该文件的代码。如果它对任何人有用,我已经在 Python 中完成了它。

from lxml import etree
import xml.etree.ElementTree as ET

class ModifyXML:
    def ChangePropertyPixbuf():


    print("start")


#____________________________________________


        tree = ET.parse("test image.glade")

        root = tree.getroot()


        for ChgValue in root.findall(".//*[@class='GtkImage']"):

            ChgValueb = ChgValue.find(".//*[@name='pixbuf']")

            if ChgValueb is not None:

                ValueOfb = ChgValueb.text

                if ChgValueb.text != "" : ChgValue.remove(ChgValueb)

                ChgValueb = ET.SubElement(ChgValue,'property')

                ChgValueb.set("name", "file")

                ChgValueb.text = ValueOfb

        tree.write("test image.glade")

#_________________________________________

print("done")


ModifyXML.ChangePropertyPixbuf()
于 2015-12-29T05:29:53.150 回答