Python(GTK):有人可以解释TreeStore,Listmodel和所有其他的区别吗? [英] Python (GTK) : Can someone explain the difference between TreeStore, Listmodel and all the rest?

查看:250
本文介绍了Python(GTK):有人可以解释TreeStore,Listmodel和所有其他的区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的头脑围绕以下概念,但是我阅读过的所有文档将所有内容捆绑在一起,而没有解释它们中的任何一个是什么,因此我不知道如何使用它们正确的。如果有人可以给出某种真实世界的比喻,这将有很大的帮助。

TreeView
ListView(这甚至是一件事情?)
TreeStore
ListStore
TreeModel
TreeIter



我的最终目标是从数据库中读取一组结果并将它们显示在一个小部件上可点击的项目。我已经完成了查找和检索部分,但将结果传递回来,显示它们并让它们更新变得更加棘手。


$ b

update:
以下是为了读取postgreSQL数据库的结果,并将其放入一个'容器'中,我可以将结果分配给输出对话框:

  result = self.lookup_player(search)#这会返回cur.fetchall的结果
打印结果#只是为了确保它返回正确的东西
如果len(结果)> 0:#确保在范围(0,len(result))中有结果
:#我使用treeiter来查看它做了什么
treeiter = store.append([result [i] [ 0],result [i] [1],str(result [i] [4])])
print result [i] [0],result [i] [1],result [i] [2 ],导致[I] [3],导致[I] [4],导致[I] [5],导致[I] [6],导致[I] [7],导致[I] [8],结果[i] [9],结果[i] [10]
else:
print找不到玩家

我在上面使用了一个ListStore,因为它似乎是最合适的。但也许我应该使用Treestore,或简单的列表或数组...
很容易看出一个字符串是什么,或者一个整数,一个浮点数,但是这些list / tree / model /商店实际上看起来像或做什么?



在C年前我不得不放弃编码,因为我无法理解多态链表是什么,而我拼命地希望避免这次与python一样的命运。

解决方案

你有上面的GtkListStore代码的正确理念:它是一个数据存储与SQL表非常类似。实际上,您会发现GtkTreeView基础结构像SQL表一样工作。您的 Gtk.ListStore()构造函数调用直接映射到 CREATE TABLE append() 直接调用映射到 INSERT ROW



重要的是要注意GtkListStore中的列从左到右编号,从0开始,而不是命名。这一点很重要。



GtkListStore和GtkTreeStore之间的区别在于后者允许您将行作为其他行的子元素,例如a中的文件夹树文件浏览器会。子行必须与父行具有相同的列格式。



GtkListStore和GtkTreeStore都是GtkTreeModel的实现,它是接口。根据我所知,Python没有像接口这样的直接概念,所以将接口设想为抽象基类,其中没有默认实现,并且必须定义每个函数。 GtkTreeModel指定了允许GtkTreeView在屏幕上显示数据的方法。



所以现在唯一的问题是如何将GtkTreeView连接到GtkTreeModel / GtkListStore / GtkTreeStore? / p>

正如您从SQL中记住的那样,表的每一列都有一个特定的数据类型。使用GtkTreView时,列中的所有单元格都具有相同的单元格渲染器。单元格渲染器使用GObject 属性绘制文本,图片,复选框等。您可能已经使用过属性: text 是GtkLabels上的一个属性, active 是GtkCheckButtons上的一个属性,等等。单元格渲染器的属性不仅指定要绘制的数据,还指定如何绘制它。



当您将属性(在函数名称中)提供给GtkTreeViewColumn(GtkTreeView中单个列的表示形式),您可以调用您要调用的函数来提供两件事情:单元格渲染器本身和属性列数对列表。

例如,假设您希望表模型的第0列提供第一列的文本。以下是创建列的方法:

  renderer = Gtk.CellRendererText()#创建文本单元格渲染器
列= Gtk.TreeViewColumn(Column)#创建列
column.pack_start(renderer,True)#加载渲染器...
column.add_attribute(渲染器,文本,0)#。 ..并告诉它从模型的第一列获取文本

现在剩下的就是将列添加到GtkTreeView并将GtkListStore设置为GtkTreeView的模型(使用 set_model())。如果一切顺利,您应该在GtkTreeView中看到您的数据。






GtkTreeView不提供自己的滚动条。请确保将您的GtkTreeView放入GtkScrolledWindow中以获取它们。

有一个GtkListBox,但它与此完全无关。



希望这有助于清理事情!如果不是,请随时指出你不明白的地方,然后我会相应地修改这个答案。




更新:回应您的评论,询问为什么GtkTreeView能够像这样工作,而不是管理所有数据本身,只需要像 add_row() set_cell_value()。这个设计有几个优点:


  • 它使得GtkTreeView自己的代码在未来更简单和更具可扩展性。

  • 它允许有特殊需求的人实现自己的GtkTreeModel作为数据存储。例如,编写足够先进或需要内存的程序的人可以创建一个与SQL数据库直接通信的GtkTreeModel,从而避免需要在任何地方复制数据。
  • 它允许相同的数据存储以供GtkTreeView的多个实例使用。

  • 它允许其他小部件使用相同的数据存储。 GtkIconView 是另一个使用GtkTreeModel存储其数据的小部件(它也使用单元格渲染器来呈现数据)。

I'm trying to get my head around the following concepts but all the documentation I've read bundles the whole lot in together without explaining what any of them really are and as a result I don't know how to use any of them properly. If someone could give some kind of real-world analogy that would help hugely.

TreeView ListView (is this even a thing?) TreeStore ListStore TreeModel TreeIter

My ultimate goal is to read a set of results from a database and display them on a widget as clickable items. I've got the lookup and retrieval parts down but passing the results back, displaying them and getting them to update is proving a lot more tricky.

update: The following is meant to read the results from a postgreSQL DB and put it into a 'container' from whence I can assign the results to an output dialog:

result = self.lookup_player(search) # this returns a result of cur.fetchall
    print result # just to make sure it returns the right things
    if len(result) > 0:  # make sure there are results
       for i in range(0, len(result)): # i used treeiter to see what it did
        treeiter=store.append([result[i][0],result[i][1],str(result[i][4])])
        print result[i][0],result[i][1],result[i][2],result[i][3],result[i][4],result[i][5],result[i][6],result[i][7],result[i][8],result[i][9],result[i][10]
    else:
       print "No players found"

I've used a ListStore in the above as it seemed like the best fit. But maybe I should have used a Treestore, or a simple list, or an array... It's easy to see what a string is, or an integer, a float, but what do these list/tree/model/stores actually look like or do?

I had to give up coding in C years ago as I just could not get my head around what a polymorphic linked list was and I desperately want to avoid the same fate with python this time around.

解决方案

You have the right idea with your GtkListStore code above: it's a data store that works very similarly to an SQL table. In fact, you'll find tha the GtkTreeView infrastructure works like SQL tables. Your Gtk.ListStore() constructor call maps directly to CREATE TABLE and your append() call maps directly to INSERT ROW.

The important thing to note is that the columns in your GtkListStore are numbered from left to right, starting at 0, rather than named. This will be important in a bit.

The difference between GtkListStore and GtkTreeStore is that the latter allows you to have rows as children of other rows, like a folder tree in a file browser would. The children rows have to have the same column format as the parent rows.

Both GtkListStore and GtkTreeStore are implementations of GtkTreeModel, which is an interface. From what I've been told, Python doesn't have an immediate concept like interfaces, so just imagine an interface as an abstract base class where there are no default implementations and every function must be defined. GtkTreeModel specifies methods which allows a GtkTreeView to display the data on screen.

So the only question now is how do you connect a GtkTreeView to a GtkTreeModel/GtkListStore/GtkTreeStore?

As you remember from SQL, each column of a table has a specific data type. With GtkTreView, all cells in a column have the same cell renderer. The cell renderer draws text, pictures, checkboxes, etc., using GObject properties. You've likely used properties already: text is a property on GtkLabels, active is a property on GtkCheckButtons, etc. Cell renderers have properties that not only specify what data to draw, but also how to draw it.

When you supply the "attributes" (in the function name) to a GtkTreeViewColumn (the representation of a single column in a GtkTreeView), you give the function you call to do the supplying two things: the cell renderer itself and a list of attribute-column number pairs.

For instance, let's say you want column 0 of the table model to provide the text for the first column. Here's how you would create the column:

renderer = Gtk.CellRendererText()            # create a text cell renderer
column = Gtk.TreeViewColumn("Column")        # create the column
column.pack_start(renderer, True)            # load the renderer...
column.add_attribute(renderer, "text", 0)    # ...and tell it to get its text from the first column of the model

Now all that's left is to add the columns to the GtkTreeView and to set your GtkListStore as the GtkTreeView's model (with set_model()). If all goes well, you should see your data in the GtkTreeView.


GtkTreeView doesn't provide its own scrollbars. Be sure to put your GtkTreeView in a GtkScrolledWindow to get them.

There is a GtkListBox, but it's entirely unrelated to this.

Hopefully this helps clear things up! If not, feel free to point out what you don't understand and I'll amend this answer accordingly.


Update: In response to your comment asking why GtkTreeView works like this, rather than managing all the data itself and just having functions like add_row() and set_cell_value(). There are a few advantages to this design:

  • It makes GtkTreeView's own code simpler and more extensible in the future.
  • It allows people with special needs to implement their own GtkTreeModel to use as the data store. For example, someone writing a sufficiently advanced or memory-hungry program can create a GtkTreeModel that communicates directly with the SQL database, avoiding the need to copy data everywhere.
  • It allows the same data store to be used by multiple instances of GtkTreeView.
  • It allows the same data store to be used by other widgets. GtkIconView is another widget that uses GtkTreeModel to store its data (and it also uses cell renderers to render the data).

这篇关于Python(GTK):有人可以解释TreeStore,Listmodel和所有其他的区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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