如何在PyGTK应用程序中嵌入电子表格/表格?
在我的应用程序中,我们想给用户展示一个典型的电子表格或表格(就像OO.O或Excel那样),然后提取出里面的数值并在内部进行处理。
有没有现成的PyGTK小部件可以做到这一点?PyGTK常见问题解答提到了GtkGrid,但是那个链接已经失效,我也找不到相关的文件。
3 个回答
0
你可以考虑嵌入一个网页浏览器,这样你可以做很多事情:http://blog.mypapit.net/2009/09/pymoembed-web-browser-in-python-gtk-application.html
3
这可能比在.Net中使用GridView要稍微麻烦一些,但Tree View这个小工具能满足你的需求,而且功能更多。你可以查看一下这个链接了解更多:PyGtk TreeView
12
GtkGrid
这个东西已经不推荐使用了,取而代之的是更强大、更灵活的GtkTreeView
。
它可以用来显示树形结构和列表。如果你想让它像表格一样工作,你需要定义一个ListStore
,这个地方会存放你的数据。同时,你还需要为每一列定义TreeViewColumn
,并用CellRenderer
来决定这些列是怎么显示的。这样分开数据和显示的方式,让你可以在单元格里放其他控件,比如文本框或图片。
GtkTreeView
非常灵活,但一开始看起来可能会觉得复杂,因为选项很多。为了帮助你理解,可以参考PyGTK教程中的相关章节(不过最好是把整个教程都读一遍,而不仅仅是这一部分)。
为了完整性,这里有一段示例代码和我系统上运行的截图:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class TreeViewColumnExample(object):
# close the window and quit
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def __init__(self):
# Create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("TreeViewColumn Example")
self.window.connect("delete_event", self.delete_event)
# create a liststore with one string column to use as the model
self.liststore = gtk.ListStore(str, str, str, 'gboolean')
# create the TreeView using liststore
self.treeview = gtk.TreeView(self.liststore)
# create the TreeViewColumns to display the data
self.tvcolumn = gtk.TreeViewColumn('Pixbuf and Text')
self.tvcolumn1 = gtk.TreeViewColumn('Text Only')
# add a row with text and a stock item - color strings for
# the background
self.liststore.append(['Open', gtk.STOCK_OPEN, 'Open a File', True])
self.liststore.append(['New', gtk.STOCK_NEW, 'New File', True])
self.liststore.append(['Print', gtk.STOCK_PRINT, 'Print File', False])
# add columns to treeview
self.treeview.append_column(self.tvcolumn)
self.treeview.append_column(self.tvcolumn1)
# create a CellRenderers to render the data
self.cellpb = gtk.CellRendererPixbuf()
self.cell = gtk.CellRendererText()
self.cell1 = gtk.CellRendererText()
# set background color property
self.cellpb.set_property('cell-background', 'yellow')
self.cell.set_property('cell-background', 'cyan')
self.cell1.set_property('cell-background', 'pink')
# add the cells to the columns - 2 in the first
self.tvcolumn.pack_start(self.cellpb, False)
self.tvcolumn.pack_start(self.cell, True)
self.tvcolumn1.pack_start(self.cell1, True)
self.tvcolumn.set_attributes(self.cellpb, stock_id=1)
self.tvcolumn.set_attributes(self.cell, text=0)
self.tvcolumn1.set_attributes(self.cell1, text=2,
cell_background_set=3)
# make treeview searchable
self.treeview.set_search_column(0)
# Allow sorting on the column
self.tvcolumn.set_sort_column_id(0)
# Allow drag and drop reordering of rows
self.treeview.set_reorderable(True)
self.window.add(self.treeview)
self.window.show_all()
def main():
gtk.main()
if __name__ == "__main__":
tvcexample = TreeViewColumnExample()
main()