python - gtk treeview - 具有实时更新的列表存储

发布于 2024-09-06 10:18:20 字数 983 浏览 1 评论 0原文

我在尝试获取实时更新时遇到树视图列表存储问题,我创建了一个示例来模拟我想做的事情。 我希望 liststore1 每次循环都更新。

http://img204.imageshack.us/i/capturadetela5.png/

应该更新树视图列“速度”并每秒给它一个不同的数字, 像下载管理器之类的东西。

import gtk
import gtk.glade
import random

builder = gtk.Builder()
builder.add_from_file('ttt.glade')
window = builder.get_object('window1')
treeview  = builder.get_object('treeview1')
store = builder.get_object('liststore1')
column_n = ['File','Size','Speed']
rendererText = gtk.CellRendererText()


for i in range(10):
    foo = random.randint(100,256)
    list_ =  [('arquivo1.tar.gz', '10MB', '%s k/s' % foo)]

for x,y in zip(column_n,range(3)):

    column = gtk.TreeViewColumn(x, rendererText, text=y)
    column.set_sort_column_id(0)    
    treeview.append_column(column)


for list_index in list_:
    store.append([list_index[0],list_index[1],list_index[2]])

window.show_all()

i am having a issue with treeview liststore trying to get a real-time update, and I created a example to simulate what I'd like to do.
I want liststore1 updated each loop.

http://img204.imageshack.us/i/capturadetela5.png/

it should update the treeview column 'speed' and give to it a different number every second,
something like a download manager.

import gtk
import gtk.glade
import random

builder = gtk.Builder()
builder.add_from_file('ttt.glade')
window = builder.get_object('window1')
treeview  = builder.get_object('treeview1')
store = builder.get_object('liststore1')
column_n = ['File','Size','Speed']
rendererText = gtk.CellRendererText()


for i in range(10):
    foo = random.randint(100,256)
    list_ =  [('arquivo1.tar.gz', '10MB', '%s k/s' % foo)]

for x,y in zip(column_n,range(3)):

    column = gtk.TreeViewColumn(x, rendererText, text=y)
    column.set_sort_column_id(0)    
    treeview.append_column(column)


for list_index in list_:
    store.append([list_index[0],list_index[1],list_index[2]])

window.show_all()

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

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

发布评论

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

评论(1

你的他你的她 2024-09-13 10:18:21

如果这是您的完整代码,则您缺少 GTK 主循环调用。

您需要做两件事(按此顺序)

1 - 将窗口的 destroy 信号连接到调用 gtk.main_quit() 的函数

def on_destroy(widget, user_data=None):
    # Exit the app
    gtk.main_quit()

window.connect('destroy', on_destroy)

2 - 启动 GTK 主循环:

gtk.main()

这是您的应用程序有效启动的位置,并且它将显示为挂起在该行,直到调用 gtk.main_quit() 为止。

更一般地说......你应该清理一下代码:)看看 " PyGTK 教程中的 Hello World" 演示 - 它基本上涵盖了这些要点以及更详细的信息。你会发现遵循它们的一般结构会有很大帮助。

如果您想要定时更新,请查看函数 timeout_addtimeout_add_seconds - 根据您的 PyGTK/PyGobject 版本,这些将位于 glibgobject 模块中。

(顺便说一句,GTKBuilder XML 文件通常具有 .ui 扩展名,尽管 Glade 并不知道。)

If that's your full code, you're missing the GTK main loop invocation.

You need to do two things (in this order)

1 - Connect your window's destroy signal to a function that calls gtk.main_quit()

def on_destroy(widget, user_data=None):
    # Exit the app
    gtk.main_quit()

window.connect('destroy', on_destroy)

2 - Start the GTK main loop:

gtk.main()

This is where your app is effectively launched, and it will appear to hang at this line until gtk.main_quit() is called.

More generally... you should clean up the code a bit there :) Look at the "Hello World" demo from the PyGTK tutorial - it basically covers those points and more in greater detail. You'll find that following their general structure for things helps immensely.

If you want timed updates, look at the functions timeout_add and timeout_add_seconds - depending on your version of PyGTK/PyGobject these will be in the glib or gobject modules.

(Incidentally, GTKBuilder XML files typically have the .ui extension, even though Glade doesn't know it.)

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