Insert table / table into PyGTK application?
In my application, we want to present the user with a typical table / table (OO.O / Excel style) and then pull the values and do something with them internally.
Is there an existing widget for PyGTK that does this? The PyGTK FAQ mentions GtkGrid , but the link is dead and I can't find the archive anywhere.
source to share
GtkGrid
deprecated in favor of a more powerful and customizable one GtkTreeView
.
It can display trees and lists. To make it work like a table, you have to define ListStore
where it will get the data, and TreeViewColumn
for each column you want to show, from CellRenderer
before, define how to show the column. This separation of data and rendering allows you to display other controls in the cell, such as text boxes or images.
GtkTreeView
very flexible, but at first it seems complicated due to the many options. To help with this, see the relevant section in the PyGTK tutorial (although you should read it in its entirety, not just this section).
For completeness, here's a sample code and a screenshot of how it works on my system:
#!/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()
source to share
It might be a little more manual than, for example, the GridView in .Net, but the Tree View widget will do what you want and more. See PyGtk TreeView
source to share
You might consider embedding a web viewer - you can do a lot with that: http://blog.mypapit.net/2009/09/pymoembed-web-browser-in-python-gtk-application.html
source to share