I'm having trouble moving the treeView selection. Specifically, when I
use move_up and move_down, the selection occasionally stops working.
When it breaks, the widget continues to show my selection, but future
calls that ask for it get no selection at all and I can't change the
selection by clicking.
The most basic code that will get me this behavior is the following:
ts, itera = self.treeview.get_selection().get_selected()
if itera:
next = ts.iter_next(itera)
ts.move_after(itera, next)
I've attached a full piece of sample code to illustrate my problem --
the code shows a simplified version of what I'd like to implement --
namely, convenient up and down buttons to move the selection (in
addition to using dnd for this).
With the attached code, I get erratic behavior. If I scroll to the
bottom and select a row, for example, I can move up and then down
(using only the buttons) without any trouble. However, if I do the
same thing with one of the top rows, it breaks immediately. In either
case, if I move a row up with my button, then select a row with the
mouse and try to move a row with a button again, it fails.
I'd like to know if anyone else gets the same behavior or has any
insight into what's going on.
Thanks,
Tom
#!/usr/bin/env python
import pygtk
#pygtk.require('2.0')
import gtk
class TreeViewExample:
# close the window and quit
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return gtk.FALSE
def __init__(self):
# Create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Treeview Example")
self.window.set_size_request(200, 200)
self.window.connect("delete_event", self.delete_event)
self.scrolledwindow = gtk.ScrolledWindow()
self.vbox = gtk.VBox()
self.hbox = gtk.HButtonBox()
self.vbox.pack_start(self.scrolledwindow, True)
self.vbox.pack_start(self.hbox, False)
self.b0 = gtk.Button(None,stock=gtk.STOCK_GO_UP)
self.b0.connect('clicked',self.upCB)
self.b1 = gtk.Button(None,stock=gtk.STOCK_GO_DOWN)
self.b1.connect('clicked',self.downCB)
self.hbox.pack_start(self.b0)
self.hbox.pack_start(self.b1)
# create a treestore with one string column to use as the model
self.treestore = gtk.TreeStore(str)
self.populate_treestore()
# create the TreeView using treestore
self.treeview = gtk.TreeView(self.treestore)
# set selection mode
self.treeview.get_selection().set_mode(gtk.SELECTION_SINGLE)
# create a CellRenderer to render the data
self.cell = gtk.CellRendererText()
# create the TreeViewColumns to display the data
self.tvcolumn = gtk.TreeViewColumn('URL', self.cell, text=0)
# add columns to treeview
self.treeview.append_column(self.tvcolumn)
# make treeview searchable
self.treeview.set_search_column(0)
# Allow sorting on the column
self.tvcolumn.set_sort_column_id(0)
self.scrolledwindow.add(self.treeview)
self.window.add(self.vbox)
self.window.show_all()
def populate_treestore (self):
for n in range(20):
iter=self.treestore.append(None)
self.treestore.set_value(iter,0,"Row %s"%n)
def upCB (self, *args):
ts, itera = self.treeview.get_selection().get_selected()
if itera:
path=ts.get_path(itera)
prev=self.path_next(path,-1)
prev_iter=ts.get_iter(prev)
ts.move_before(itera,prev_iter)
self.treeview.get_selection().select_path(prev)
else: print "No selection!"
def downCB (self, *args):
ts, itera = self.treeview.get_selection().get_selected()
if itera:
next = ts.iter_next(itera)
ts.move_after(itera,next)
next_path=ts.get_path(next)
self.treeview.get_selection().select_path(self.path_next(next_path,1))
else: print "No selection!"
def path_next (self, path, inc=1):
"""Return the path NEXT rows after PATH. Next can be negative, in
which case we get previous paths."""
print "path_next called w/ path=%s, inc=%s"%(path,inc)
next=list(path[0:-1])
last=path[-1]
last += inc
if last < 0:
last=0
next.append(last)
next=tuple(next)
print "path_next returning ",next," to be selected"
return next
def main():
gtk.main()
if __name__ == "__main__":
treeviewex = TreeViewExample()
main()
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/