#coding: utf-8
import gtk
import MySQLdb as mdb
import sys
try:
conn = mdb.connect('localhost', '', '', 'catalogo')
cr = conn.cursor()
cr.execute("SELECT * FROM libros l INNER JOIN categoria c ON l.idCategoria = c.idCategoria")
rows = cr.fetchall()
desc = cr.description
cr.close()
conn.close()
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
class PyApp(gtk.Window):
def __init__(self):
#Llamada explicita al constructor de la clase padre
super(PyApp, self).__init__()
#Estas son las cabeceras
self.headers = [desc[0][0], desc[1][0], desc[2][0], desc[3][0], desc[7][0], desc[5][0]]
#Hacemos algunos ajustes a la apariencia de la ventana
self.set_size_request(550, 450)
self.set_position(gtk.WIN_POS_CENTER)
self.set_title("ListView")
vbox = gtk.VBox(False, 8)
sw = gtk.ScrolledWindow()
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
vbox.pack_start(sw, True, True, 0)
store = self.create_model()
treeView = gtk.TreeView(store)
treeView.connect("row-activated", self.on_activated)
treeView.set_rules_hint(True)
sw.add(treeView)
self.create_columns(treeView)
self.statusbar = gtk.Statusbar()
vbox.pack_start(self.statusbar, False, False, 0)
self.add(vbox)
self.show_all()
self.connect("destroy", gtk.main_quit)
def create_model(self):
store = gtk.ListStore(*[str]*len(self.headers))
for item in rows:
store.append(item[0], item[1], item[2], item[3], item[7], item[5])
return store
def create_columns(self, treeView):
for index, header in enumerate(self.headers):
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn(header, rendererText, text=index)
column.set_sort_column_id(index)
treeView.append_column(column)
def on_activated(self, widget, row, col):
model = widget.get_model()
text = ', '.join(i for i in model[row])
self.statusbar.push(0, text)
PyApp()
gtk.main()