Hallo Pythonistas,
ich habe hier eine GUI mit GTK in dem eine Liste mit Liststore und
Treeview gefüllt ist.
Wenn nun eine Eintrag der Liste angeklickt wurde, brauche ich ein Index.
Da sich die Spalten sortieren lassen, kann ich die Cursor-Position nicht
nehmen. Hierzu habe ich eine durchnummerierte Spalte anlegt, die nicht
angezeigt wird.
Meine Frage: Ist das so richtig? Denn ich konnte keine andere Methode
finden.
Mathias
hier Auszüge
<pre>
# create a liststore with string column to use as the model
self.myListstore = gtk.ListStore(int, str, str, 'gboolean')
# create the TreeView using liststore
self.treeview = gtk.TreeView(self.myListstore)
self.treeview.connect("row-activated", self.Call_ListClick)
col = {
#0:['Nr',0],
1:['Time',1],
2:['Title',2]}
for key, val in col.iteritems():
# create the TreeViewColumns to display the data
tvcolumn = gtk.TreeViewColumn(val[0])
# add columns to treeview
self.treeview.append_column(tvcolumn)
# create a CellRenderers to render the data
cell = gtk.CellRendererText()
# add the cells to the columns
tvcolumn.pack_start(cell, True)
# content in
tvcolumn.set_attributes(cell, text=val[1])
# Allow sorting on the column
tvcolumn.set_sort_column_id(key)
def Call_ListClick(self, *args):
"""Function: If a item is klicked."""
cursor = self.treeview.get_cursor()
# Get the selection iter
selected = self.treeview.get_selection()
model, selection_iter = selected.get_selected()
val = self.myListstore.get_value(selection_iter, 0)
print val
</pre>