python - gtk treeview - 具有实时更新的列表存储
我在尝试获取实时更新时遇到树视图列表存储问题,我创建了一个示例来模拟我想做的事情。 我希望 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这是您的完整代码,则您缺少 GTK 主循环调用。
您需要做两件事(按此顺序)
1 - 将窗口的
destroy
信号连接到调用gtk.main_quit()
的函数2 - 启动 GTK 主循环:
这是您的应用程序有效启动的位置,并且它将显示为挂起在该行,直到调用
gtk.main_quit()
为止。更一般地说......你应该清理一下代码:)看看 " PyGTK 教程中的 Hello World" 演示 - 它基本上涵盖了这些要点以及更详细的信息。你会发现遵循它们的一般结构会有很大帮助。
如果您想要定时更新,请查看函数 timeout_add 和 timeout_add_seconds - 根据您的 PyGTK/PyGobject 版本,这些将位于
glib
或gobject
模块中。(顺便说一句,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 callsgtk.main_quit()
2 - Start the GTK main loop:
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
orgobject
modules.(Incidentally, GTKBuilder XML files typically have the
.ui
extension, even though Glade doesn't know it.)