ListStore/TreeStore 中的自定义对象

发布于 2024-11-25 02:56:49 字数 340 浏览 8 评论 0原文

我有一个类 A 的对象列表 L。该类实现了 __str__/__repr__,因此每个对象都有它自己的字符串表示形式(不一定是唯一的)。我在 pygtk 中有一个 GUI,其中有一个只有一列的 TreeView 小部件。我想用 L 中对象的字符串表示形式填充它,但随后我想将所选项目作为对象而不是字符串获取。有没有办法让 TreeView 存储对象列表,但将它们显示为字符串?如果没有,那么了解 TreeView 中选择了哪些对象的最佳方法是什么? 问题还在于,根据某些条件,我可以不使用整个 L 而是使用它的某些子列表来填充 TreeView,因此 TreeView 中的项目索引不会与 L 中的项目索引相对应。

I have a list L of objects of my class A. This class implements __str__/__repr__, so each object has it's own string representation (not necessary unique). I have a GUI in pygtk, where I have a TreeView widget with only one column. I want to populate it with string representations of the objects in L, but then I want to get selected items as objects, not as string. Is there a way to make TreeView to store list of objects, but display them as stings? If not, then what is the best way to know what objects are selected in the TreeView?
The problem also is that depending on some conditions I can populate TreeView not with the whole L, but with some sublist of it, and so indexes of items in TreeView won't correspond to ones in L.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷你仙 2024-12-02 02:56:49

您可以将对象存储在一列中 (gobject.TYPE_pyOBJECT),将字符串表示形式存储在第二列中,然后仅在树视图中显示第二列。与此处所做的类似: http://www.learningpython.com/2006/09 /02/扩展我们的pygtk应用程序/

You could store the object in one column (gobject.TYPE_PYOBJECT) and the string representation in a second column, and then only display the second column in your treeview. Similar to what's done here: http://www.learningpython.com/2006/09/02/extending-our-pygtk-application/

不必了 2024-12-02 02:56:49

如果您的 Glade 是 3.7.0 或更高版本,您可以键入“PyObject”(不带引号)作为 ListStore 的列类型。然后使用 set_cell_data_func 从模型中检索对象并将其表示作为文本传递给 CellRenderer。无需同步字符串列,也无需担心索引。

If your Glade is 3.7.0 or newer you can type "PyObject" (without the quotes) as the column type for you ListStore. Then use set_cell_data_func to retrieve an object from the model and pass its representation to the CellRenderer as text. No string columns to synchronize and no indexes to worry about.

夜夜流光相皎洁 2024-12-02 02:56:49

如果字符串是唯一的,您可以通过使用字符串作为键,使用字典将字符串与对象链接起来。在这种情况下,您可以通过字符串找到对象。

If the strings are unique you can use a dictionary to link the strings with the objects by using the strings as keys. In this case you can find the objects by its string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文