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

发布于 2024-08-05 00:14:06 字数 347 浏览 3 评论 0原文

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

PyGTK 是否有一个预先存在的小部件可以执行此操作? PyGTK 常见问题解答提到GtkGrid,但链接已失效,我在任何地方都找不到 tarball。

In my application, we want to present the user with a typical spreadsheet/table (OO.O/Excel-style), and then pull out the values and do something with them internally.

Is there a preexisting widget for PyGTK that does this? The PyGTK FAQ mentions GtkGrid, but the link is dead and I can't find a tarball anywhere.

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

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

发布评论

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

评论(3

野生奥特曼 2024-08-12 00:14:06

GtkGrid 已被弃用,取而代之的是更强大、更可定制的 GtkTreeView

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

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

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

Screenshot-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()

GtkGrid is deprecated in favor of the more powerful and more customizable GtkTreeView.

It can display trees and lists. To make it work like a table, you must define a ListStore where it will take the data from, and TreeViewColumns for each column you want to show, with CellRenderers to define how to show the column. This separation of data and render allows you to render other controls on the cell, like text boxes or images.

GtkTreeView is very flexible, but it seems complex at first because of the many options. To help with that, there's the relevant section in the PyGTK tutorial (although you should read it entirely, not just this section).

For completeness, here's an example code and a screenshot of it running in my system:

Screenshot-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()
云醉月微眠 2024-08-12 00:14:06

它可能比 .Net 中的 GridView 等需要更多的手动操作,但是 Tree View 小部件可以满足您的需要,甚至更多。请参阅 PyGtk TreeView

It's perhaps a little more manual than, for instance, a GridView in .Net, but the Tree View widget will do what you need and much more. See PyGtk TreeView

盗琴音 2024-08-12 00:14:06

您可能会考虑嵌入网页查看器 - 您可以用它做很多事情:http://blog.mypapit.net/2009/09/pymoembed-web-browser-in-python-gtk-application.html

You might consider embedding a web page viewer - you can do a lot with that: http://blog.mypapit.net/2009/09/pymoembed-web-browser-in-python-gtk-application.html

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