树视图中的行选择
Row selection in treeview
每次随着行更新以下代码select所有行。
child_id =treeview.get_children()
treeview.selection_set(child_id)
我不能只 select 使用控件的特定行并且每次只有 selected 行再次获得 selects
示例代码:
from tkinter import ttk
import tkinter
import threading
def main():
root = tkinter.Tk()
ccols = ('num1','num2')
treeview = ttk.Treeview(root, columns=ccols)
for col in ccols:
treeview.heading(col, text=col)
treeview.grid(row=8, column=0)
def sample():
for i in range(50):
treeview.delete(*treeview.get_children())
a=(treeview.insert("","end",values=(i,0)))
a=(treeview.insert("","end",values=(i,0)))
a=(treeview.insert("","end",values=(i,0)))
child_id =treeview.get_children()
treeview.selection_set(child_id)
threading.Timer(4.0, sample).start()
sample()
root.mainloop()
if __name__ == '__main__':
main()
您可以保存 selected 行的当前索引。然后在更新树视图后,通过保存的索引获取行 ID,并使用行 ID select 获取行:
def sample():
# save the indexes of selected rows
indexes = [treeview.index(id) for id in treeview.selection()]
# update treeview
for i in range(50):
treeview.delete(*treeview.get_children())
treeview.insert("", "end", values=(i,0))
treeview.insert("", "end", values=(i,0))
treeview.insert("", "end", values=(i,0))
# select the rows using the saved indexes
for idx in indexes:
child_id = treeview.get_children()[idx]
treeview.selection_add(child_id)
threading.Timer(4.0, sample).start()
每次随着行更新以下代码select所有行。
child_id =treeview.get_children()
treeview.selection_set(child_id)
我不能只 select 使用控件的特定行并且每次只有 selected 行再次获得 selects
示例代码:
from tkinter import ttk
import tkinter
import threading
def main():
root = tkinter.Tk()
ccols = ('num1','num2')
treeview = ttk.Treeview(root, columns=ccols)
for col in ccols:
treeview.heading(col, text=col)
treeview.grid(row=8, column=0)
def sample():
for i in range(50):
treeview.delete(*treeview.get_children())
a=(treeview.insert("","end",values=(i,0)))
a=(treeview.insert("","end",values=(i,0)))
a=(treeview.insert("","end",values=(i,0)))
child_id =treeview.get_children()
treeview.selection_set(child_id)
threading.Timer(4.0, sample).start()
sample()
root.mainloop()
if __name__ == '__main__':
main()
您可以保存 selected 行的当前索引。然后在更新树视图后,通过保存的索引获取行 ID,并使用行 ID select 获取行:
def sample():
# save the indexes of selected rows
indexes = [treeview.index(id) for id in treeview.selection()]
# update treeview
for i in range(50):
treeview.delete(*treeview.get_children())
treeview.insert("", "end", values=(i,0))
treeview.insert("", "end", values=(i,0))
treeview.insert("", "end", values=(i,0))
# select the rows using the saved indexes
for idx in indexes:
child_id = treeview.get_children()[idx]
treeview.selection_add(child_id)
threading.Timer(4.0, sample).start()