Python GTK3 Treeview向上或向下移动选择 [英] Python GTK3 Treeview Move Selection up or down

查看:492
本文介绍了Python GTK3 Treeview向上或向下移动选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这个想法是我可以有一个向上和向下的按钮来将选择向上移动一行或向下一行。



我的Treeview使用了一个ListStore。不知道这是否重要。首先,我将使用C代码,因为这就是我熟悉的内容。如果您在将问题转换为Python时遇到问题,请说明一下,我会尽我所能提供帮助。



您希望为此使用的类是 GtkTreeSelection 。基本上,你要做的是:


  1. 获取视图的选择对象( gtk_tree_view_get_selection

  2. 获取当前选中的 GtkTreeIter gtk_tree_selection_get_selected )。 li>
  3. 获得下一个/上一个iter( gtk_tree_model_iter_next / previous

  4. 如果前一个函数返回true),使它成为当前选中的一个( gtk_tree_selection_select_iter

在我的小测试程序中,down按钮的回调如下所示:

  static void on_down GtkWidget * btn,gpointer user_data)
{
GtkTreeSelection * sel = GTK_TREE_SELECTION(user_data);
GtkTreeModel * model;
GtkTreeIter current;

gtk_tree_selection_get_selected(sel,& model,& current);
if(gtk_tree_model_iter_next(model,& current))
gtk_tree_selection_select_iter(sel,& current);



连接时,我将TreeSelection对象传递给回调函数。



编辑:这就是Samuel Taylor将上述内容翻译成Python的方式:

  TreeView = Gtk.TreeView()
list = Gtk.ListStore(str,str)
TreeView.set_model(list)

def down(widget):
selection = TreeView.get_selection()
sel = selection.get_selected()
if not sel [1] == None:
next = list.iter_next(sel [1])
if next:
selection.select_iter(next)


How do I move a selection up or down in a Treeview?

The idea is that I can have an up and down buttons to move the selection up a row or down a row.

My Treeview is using a ListStore. Not sure if that matters.

解决方案

First off, I will be using C code as that's what I'm familiar with. Should you have problems translating it to Python, then say so, and I will do my best to help.

The class you want to use for this is GtkTreeSelection. Basically, what you do is:

  1. Get the selection object of the view (gtk_tree_view_get_selection)
  2. Get the currently selected GtkTreeIter (gtk_tree_selection_get_selected).
  3. Aquire the next/previous iter (gtk_tree_model_iter_next/previous)
  4. If there is one (ie. if the previous function returned true), make it the currently selected one (gtk_tree_selection_select_iter)

In my little test program, the callback for the "down" button looked like this:

static void on_down(GtkWidget *btn, gpointer user_data)
{
    GtkTreeSelection *sel = GTK_TREE_SELECTION(user_data);
    GtkTreeModel *model;
    GtkTreeIter current;

    gtk_tree_selection_get_selected(sel, &model, &current);
    if (gtk_tree_model_iter_next(model, &current))
        gtk_tree_selection_select_iter(sel, &current);
}

(here is the full program for reference)

When connecting, I passed the TreeSelection object to the callback.

Edit: This is how Samuel Taylor translated the above to Python:

TreeView = Gtk.TreeView()
list = Gtk.ListStore(str, str)
TreeView.set_model(list)

def down(widget):
    selection = TreeView.get_selection()
    sel = selection.get_selected()
    if not sel[1] == None:
        next = list.iter_next(sel[1])
        if next:
            selection.select_iter(next)

这篇关于Python GTK3 Treeview向上或向下移动选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆