Python GTK3 Treeview向上或向下移动选择 [英] Python GTK3 Treeview Move Selection up or down
问题描述
这个想法是我可以有一个向上和向下的按钮来将选择向上移动一行或向下一行。
我的Treeview使用了一个ListStore。不知道这是否重要。首先,我将使用C代码,因为这就是我熟悉的内容。如果您在将问题转换为Python时遇到问题,请说明一下,我会尽我所能提供帮助。
您希望为此使用的类是 GtkTreeSelection
。基本上,你要做的是:
- 获取视图的选择对象(
gtk_tree_view_get_selection
- 获取当前选中的
GtkTreeIter
(gtk_tree_selection_get_selected
)。 li>
- 获得下一个/上一个iter(
gtk_tree_model_iter_next / previous
) - 如果前一个函数返回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的方式: 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 In my little test program, the callback for the "down" button looked like this: (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:
这篇关于Python GTK3 Treeview向上或向下移动选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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)
GtkTreeSelection
. Basically, what you do is:
gtk_tree_view_get_selection
)GtkTreeIter
(gtk_tree_selection_get_selected
).gtk_tree_model_iter_next/previous
)gtk_tree_selection_select_iter
)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, ¤t);
if (gtk_tree_model_iter_next(model, ¤t))
gtk_tree_selection_select_iter(sel, ¤t);
}
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)