PyGTK 如何“按需加载”数据到树视图

发布于 2024-10-19 18:09:28 字数 193 浏览 7 评论 0 原文

我正在使用 PyGTK TreeView (作为列表)来存储我的数据。有没有办法通过浏览列表来加载更多数据按需

例如

我有一个数据库表,其中有 200 000 条记录。在应用程序启动时,从表中加载 200 条记录。当我滚动时将更多数据下载到树视图。

I'm using PyGTK TreeView (as list) for my data. Is there any way to load more data on demand by going throu the list.

For example:

I have database table where is 200 000 records. At application start- loads 200 records from table. when i scroll download more data to treeView.

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

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

发布评论

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

评论(2

小红帽 2024-10-26 18:09:28

我相信您可以执行以下操作:

  1. 向您的模型添加空行,我相信您应该使用 GtkListStore

  2. 为树形视图单元渲染器定义一个 数据函数

  3. 在数据函数中检查当前模型行是否已加载,如果没有则从 SQL 加载。我想你可以在这里使用兑现并一次加载多于一行。

以下是如何设置数据功能的示例:

cell = gtk.CellRendererText()        
column = gtk.TreeViewColumn('column0', cell, text=0)   
column.set_cell_data_func(cell, self.load_data)
self.list.append_column(column)

...

def load_data(self, column, cell, model, iter):
    # load data for the given model row
    print 'load data ' + model.get_value(iter, 0)

希望这有帮助,问候

I believe you could do following:

  1. Add empty lines to your model, I believe you should be using GtkListStore

  2. For the treeview cell renderer define a data function.

  3. In the data function check if current model row is loaded and load it from SQL if it's not. I guess you can use cashing here and load more then one row at once.

Below is an example of how to set up the data function:

cell = gtk.CellRendererText()        
column = gtk.TreeViewColumn('column0', cell, text=0)   
column.set_cell_data_func(cell, self.load_data)
self.list.append_column(column)

....

def load_data(self, column, cell, model, iter):
    # load data for the given model row
    print 'load data ' + model.get_value(iter, 0)

hope this helps, regards

鞋纸虽美,但不合脚ㄋ〞 2024-10-26 18:09:28

这是我如何实现树视图(列表)的按需加载
我的 Gtk.TreeView 是 Gtk.ScrolledWindow 的子级(一切都是用空地构建的,但这并不重要)

我已将树视图渲染设置为 Serge

cell = gtk.CellRendererText()
col = gtk.TreeViewColumn("Column header text")
col.pack_start(cell, True)
col.set_attributes(cell,text=0)
col.set_cell_data_func(cell, self._loadDataTest, treeView)
treeView.append_column(col)

要处理的函数:

def _loadDataTest(self, column, cell, model, iter, widget):
    pageSize = widget.get_parent().get_vadjustment().get_page_size()+20
    adjGap = widget.get_parent().get_vadjustment().get_upper() - widget.get_parent().get_vadjustment().get_value()
    if widget.is_focus() and model.get_path(iter)[0] >= len(model)-2 and adjGap < pageSize:
        for i  in range(0, 20): #simulate additional data
            model.append(["row%s"%len(model)-1])
        time.sleep(0.05) #to pause execution for a while, no need to add data fow times

现在在“pre-last”(从底部第二个)时加载新数据row 可见,scrollBar 几乎位于底部。就我而言,睡眠用于在 gtk.widgets 刷新时暂停。

通过键盘导航、鼠标拖动滚动条或鼠标单击滚动条按钮来工作
希望这会对某人有所帮助

Here is how i managed to achieve load-on-demand for treeview (list)
My Gtk.TreeView is a child of Gtk.ScrolledWindow (everything is built with glade, but that is not important)

I've set up my treeview rendering as Serge said:

cell = gtk.CellRendererText()
col = gtk.TreeViewColumn("Column header text")
col.pack_start(cell, True)
col.set_attributes(cell,text=0)
col.set_cell_data_func(cell, self._loadDataTest, treeView)
treeView.append_column(col)

Function to handle:

def _loadDataTest(self, column, cell, model, iter, widget):
    pageSize = widget.get_parent().get_vadjustment().get_page_size()+20
    adjGap = widget.get_parent().get_vadjustment().get_upper() - widget.get_parent().get_vadjustment().get_value()
    if widget.is_focus() and model.get_path(iter)[0] >= len(model)-2 and adjGap < pageSize:
        for i  in range(0, 20): #simulate additional data
            model.append(["row%s"%len(model)-1])
        time.sleep(0.05) #to pause execution for a while, no need to add data fow times

Now new data is loaded when "pre-last" (second from bottom) row is visible and scrollBar is almost at bottom. In my case, sleep is used to pause while gtk.widgets are refreshed.

Works by keyboard navigation, by mouse drag of scrollBar or mouse clicking on scrollbar button scrollbar button
Hope this will help someone

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