Python PyGOobject treeview: confirm edit after move between cells with Tab key
Asked Answered
S

2

1

After searching for a long time I found a solution (pretty simple) to move between cells of a treeview grid using Tab key and mantaining cells in edit mode.

Now I've got a problem: cell edit confirmation happens only after pressing Enter key. If I press Tab key a editing_canceled event appears to be triggered.

How to solve it? How to permit the data confirmation also on tab key press?

This is my event handler for treeview key-press-event:

def key_tree_Tab(self, treeview, event,namewidget):
    path, col = treeview.get_cursor() 
    ## only visible columns!! 
    columns = [c for c in treeview.get_columns() if c.get_visible()] 
    colnum = columns.index(col)     

    if event.keyval==65289:

        if colnum + 1 < len(columns): 
            next_column = columns[colnum + 1]               
            treeview.set_cursor(path,next_column,start_editing=True)                                    


        else: 
            tmodel = treeview.get_model() 
            titer = tmodel.iter_next(tmodel.get_iter(path)) 
            if titer is None: 
                titer = tmodel.get_iter_first() 
            path = tmodel.get_path(titer) 
            next_column = columns[0] 
            treeview.set_cursor(path,next_column,start_editing=True)

    return True

Thanks to all!!!!

Stinson answered 19/3, 2013 at 11:7 Comment(0)
T
0

I know this thread is long ago. I tried your code with current version of Python3 and Gtk3 and does not work. It works only with new rows. The existing rows do not tab to next cell. If I add "return True" then every cell can Tab even with existing cells, but none gets updated.

Thresher answered 31/10, 2017 at 0:27 Comment(2)
Hi! So sad... The code is quite old and unmaintained at the moment and it's based on python 2. Maybe someone solve the issue at library level. Do you try to remove the timeout add or to increment it? Please let me know if you solve the issue!Stinson
increment timeout does not work. and remove timeout does not tab at allThresher
S
1

A great person found a solution: call the set_cursor method from a gobject timeout!!!

I port it from pygtk to pygobject, and I adapt that at the method post previously.

So,for all those who need it, the code:

def key_tree_Tab(self, treeview, event,namewidget):
    keyname = Gdk.keyval_name(event.keyval)

    path, col = treeview.get_cursor() 
    ## only visible columns!! 
    columns = [c for c in treeview.get_columns() if c.get_visible()] 
    colnum = columns.index(col)     


    if keyname=="Tab" or keyname=="Esc":

        if colnum + 1 < len(columns): 
            next_column = columns[colnum + 1]               

        else: 
            tmodel = treeview.get_model() 
            titer = tmodel.iter_next(tmodel.get_iter(path)) 
            if titer is None: 
                titer = tmodel.get_iter_first() 
            path = tmodel.get_path(titer) 
            next_column = columns[0] 


        if keyname == 'Tab':
            #Thank you Jordan!!!!!! Great hack!
            GLib.timeout_add(50,
                            treeview.set_cursor,
                            path, next_column, True)
        elif keyname == 'Escape':
            pass

Really thanks to Jordan Callicoat for these beatiful piece of hack!

Greetings.

Stinson answered 22/3, 2013 at 9:16 Comment(0)
T
0

I know this thread is long ago. I tried your code with current version of Python3 and Gtk3 and does not work. It works only with new rows. The existing rows do not tab to next cell. If I add "return True" then every cell can Tab even with existing cells, but none gets updated.

Thresher answered 31/10, 2017 at 0:27 Comment(2)
Hi! So sad... The code is quite old and unmaintained at the moment and it's based on python 2. Maybe someone solve the issue at library level. Do you try to remove the timeout add or to increment it? Please let me know if you solve the issue!Stinson
increment timeout does not work. and remove timeout does not tab at allThresher

© 2022 - 2024 — McMap. All rights reserved.