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

时光怂恿深爱的人放手 提交于 2019-12-23 02:19:25

问题


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!!!!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/15497766/python-pygoobject-treeview-confirm-edit-after-move-between-cells-with-tab-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!