动态更改 gtkListStore 模型
所以我终于开始了解 PyGTK 的真正工作原理,并且我正在开发一个简单的 SQLite 管理器应用程序。
为了显示查询结果,我使用了 gtk.ScrolledWindow 和 gtk.TreeView。我已经设法根据需要添加甚至更改 ListStore 对象中的值,我不能做的是更改 ListStore 对象的模型。
例如,在一个查询中,我可能会得到两列的结果:
self.query_results = gtk.ListStore(str,str)
self.query_results.append(["1"],["John Doe"])
在另一查询中,我可能会得到更多列:
self.query_results = gtk.ListStore(str,str,str,str)
self.query_results.append(["20"],["Jane"],["[email protected]"],["password"])
我知道我可以使用类似的东西
self.query_results = gtk.ListStore(*[str]*len(columns))
但显然 gtk.ListStore 在创建后无法更改其模型,那么我将如何更新这个查询表?每次运行不同的查询时,是否需要销毁 Treeview 并重新绘制它?
谢谢,这是我在 StackOverflow 上的第一个问题,所以请原谅我没有遵循的任何错误/发布指南。
So I finally got around to understand how PyGTK really works and I'm working on a simple SQLite manager app.
To show the result of a query I'm using a gtk.ScrolledWindow with a gtk.TreeView. I've managed to add and even change the values in the ListStore object as I need, what I can't do is change the model of the ListStore object.
For example in one query I may have a result of two columns:
self.query_results = gtk.ListStore(str,str)
self.query_results.append(["1"],["John Doe"])
And in another one I may get more columns:
self.query_results = gtk.ListStore(str,str,str,str)
self.query_results.append(["20"],["Jane"],["[email protected]"],["password"])
I know I can use stuff like
self.query_results = gtk.ListStore(*[str]*len(columns))
But aparently gtk.ListStore cannot have it's model changed after being created, so how would I update this query table? Do I need to destroy the Treeview and draw it again everytime I run a different query?
Thanks and this is my first question on StackOverflow so excuse any mistakes/posting guidelines I didn't follow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
gtk.TreeView.set_model
将新模型分配给 TreeView。因此,您可以为每组结果生成一个新模型,然后设置 TreeView 以显示该模型。You can use
gtk.TreeView.set_model
to assign a new model to a TreeView. So you can generate a new model for each set of results, and then set the TreeView to show that model.