[英]Getting the Gtk.TreePath of the first row in a Gtk.TreeView
I want to have the first row of a Gtk.TreeView
selected upon a button press.我想在按下按钮时选择
Gtk.TreeView
的第一行。 I'm aware that this is possible using row_activated
, however row_activated
takes a Gtk.TreePath
and a Gtk.TreeViewColumn
(which I assume can be set to None
).我知道这可以使用
row_activated
,但是row_activated
需要一个Gtk.TreePath
和一个Gtk.TreeViewColumn
(我假设可以设置为None
)。 The issue is that I do not know how to get the Gtk.TreePath
of the first row.问题是我不知道如何获取第一行的
Gtk.TreePath
。
class Main(Gtk.Window):
def __init__(self):
super().__init__()
liststore = Gtk.ListStore(str)
liststore.append(['first_entry'])
liststore.append(['second_entry'])
liststore.append(['third_entry'])
treeview = Gtk.TreeView()
treeview.set_model(liststore)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn('Item', renderer, text=0)
treeview.append_column(column)
button = Gtk.Button(label='button')
box = Gtk.Box()
box.set_orientation(Gtk.Orientation.VERTICAL)
box.add(button)
box.add(treeview)
self.add(box)
if __name__ == '__main__':
main = Main()
main.show_all()
Gtk.main()
In this case, when the button is pressed, I want first_entry
(which is the first row) to be selected在这种情况下,当按下按钮时,我希望
first_entry
(即第一行)
I found treeview.set_cursor(0)
in question: Python + GTK: How to set a selected row on gtk.treeview - Stack Overflow and I think it can be better solution. I found
treeview.set_cursor(0)
in question: Python + GTK: How to set a selected row on gtk.treeview - Stack Overflow and I think it can be better solution.
BTW: you have to use self.
顺便说一句:你必须使用
self.
to access treeview
in function executed by button.通过按钮执行
treeview
中的 treeview 访问。
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class Main(Gtk.Window):
def __init__(self):
super().__init__()
liststore = Gtk.ListStore(str)
liststore.append(['first_entry'])
liststore.append(['second_entry'])
liststore.append(['third_entry'])
self.treeview = Gtk.TreeView()
self.treeview.set_model(liststore)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn('Item', renderer, text=0)
self.treeview.append_column(column)
button = Gtk.Button(label='button')
button.connect('clicked', self.select_first)
box = Gtk.Box()
box.set_orientation(Gtk.Orientation.VERTICAL)
box.add(button)
box.add(self.treeview)
self.add(box)
def select_first(self, event):
self.treeview.set_cursor(0)
if __name__ == '__main__':
main = Main()
main.show_all()
Gtk.main()
Doc for set_cursor() for C/C++ (but can be useful).用于 C/C++ 的set_cursor()的文档(但可能很有用)。
It seems original set_cursor()
also needs TreePath
but in Python it works with index
.似乎原始
set_cursor()
也需要TreePath
但在 Python 它适用于index
。 Other functions don't have to work with index
instead of TreePath
其他函数不必使用
index
而不是TreePath
EDIT:编辑:
I found you can create TreePath
using index
- as list
or string
我发现您可以使用
index
创建TreePath
- 作为list
或string
def select_first(self, event):
path = Gtk.TreePath.new_from_indices([0]) # it needs list because trees can be nestes, ie. [0,5,3]
#path = Gtk.TreePath.new_from_string("0") # for nested trees it can be ie. "0:5:3"
selection = self.treeview.get_selection()
selection.select_path(path)
EDIT:编辑:
If you use self.
如果你使用
self.
when you create self.column = Gtk.TreeViewColumn('Item', renderer, text=0)
then you can use it with row_activated()
but this doesn't select row on my computer (but doesn't raise error)当您创建
self.column = Gtk.TreeViewColumn('Item', renderer, text=0)
时,您可以将它与row_activated()
一起使用,但这不是 select 在我的计算机上的行(但不会引发错误)
def select_first(self, event):
path = Gtk.TreePath.new_from_indices([0])
self.treeview.row_activated(path, self.column)
Full code which I used for tests:我用于测试的完整代码:
# https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeSelection.html#gtk-treeselection
# https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreePath.html#Gtk.TreePath.new_from_indices
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class Main(Gtk.Window):
def __init__(self):
super().__init__()
self.connect("destroy", Gtk.main_quit)
liststore = Gtk.ListStore(str)
liststore.append(['first_entry'])
liststore.append(['second_entry'])
liststore.append(['third_entry'])
self.treeview = Gtk.TreeView()
self.treeview.set_model(liststore)
renderer = Gtk.CellRendererText()
self.column = Gtk.TreeViewColumn('Item', renderer, text=0)
self.treeview.append_column(self.column)
button1 = Gtk.Button(label='First')
button1.connect('clicked', self.select_first)
button2 = Gtk.Button(label='Second')
button2.connect('clicked', self.select_second)
button3 = Gtk.Button(label='Third')
button3.connect('clicked', self.select_third)
box = Gtk.Box()
box.set_orientation(Gtk.Orientation.VERTICAL)
box.add(button1)
box.add(button2)
box.add(button3)
box.add(self.treeview)
self.add(box)
def select_first(self, event):
self.treeview.set_cursor(0)
def select_second(self, event):
path = Gtk.TreePath.new_from_indices([1]) # it needs list because trees can be nestes, ie. [0,5,3]
path = Gtk.TreePath.new_from_string("1") # for nested trees it can be ie. "0:5:3"
selection = self.treeview.get_selection()
selection.select_path(path)
def select_third(self, event):
path = Gtk.TreePath.new_from_indices([2]) # it needs list because trees can be nestes, ie. [0,5,3]
self.treeview.row_activated(path, self.column)
if __name__ == '__main__':
main = Main()
main.show_all()
Gtk.main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.