繁体   English   中英

在PyGTK应用程序中嵌入电子表格/表格?

[英]Embed a spreadsheet/table in a PyGTK application?

在我的应用程序中,我们希望向用户呈现典型的电子表格/表格(OO.O / Excel样式),然后提取值并在内部对它们执行某些操作。

PyGTK是否有一个预先存在的小部件可以做到这一点? PyGTK常见问题解答提到GtkGrid ,但链接已经死了,我无法在任何地方找到tarball。

GtkGrid不赞成使用更强大,更可定制的GtkTreeView

它可以显示树和列表。 要使其像表一样工作,您必须定义一个ListStore ,它将从中获取数据, TreeViewColumn用于您要显示的每个列, CellRenderer用于定义如何显示列。 这种数据和渲染的分离允许您在单元格上渲染其他控件,如文本框或图像。

GtkTreeView非常灵活,但由于有很多选项,它起初看起来很复杂。 为了帮助解决这个问题, PyGTK教程中相关部分 (尽管您应该完全阅读它,而不仅仅是本节)。

为了完整起见,这是一个示例代码及其在我的系统中运行的屏幕截图:

截图 -  TreeViewColumn Example.png

#!/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()

它可能比.Net中的GridView更加手动,但树视图小部件将完成您所需的操作以及更多功能。 请参阅PyGtk TreeView

你可以考虑嵌入一个网页浏览器 - 你可以做很多事情: http//blog.mypapit.net/2009/09/pymoembed-web-browser-in-python-gtk-application.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM