I want to get the first value when selected the particular row. It looks very simple but i am not getting where i am missing out something. i am getting the first value in the print statement but after that getting the following error. please help me with this. Thank you.
1
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\Desktop\select.py", line 31, in on_tree_select
print(str(curItem['values'][0]))
IndexError: string index out of range
Sample code:
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():
indexes = [treeview.index(id) for id in treeview.selection()]
for i in range(2):
treeview.delete(*treeview.get_children())
treeview.insert("", "end", values=(i,0))
for idx in indexes:
child_id = treeview.get_children()[idx]
treeview.selection_add(child_id)
threading.Timer(4.0, sample).start()
def on_tree_select( event):
curItem = treeview.item(treeview.focus())
print(str(curItem['values'][0])) #getting output as 1 but then error
treeview.bind("<<TreeviewSelect>>", on_tree_select)
sample()
root.mainloop()
if __name__ == '__main__':
main()
Try to use try - except
:
try:
print(str(curItem['values'][0])) #getting output as 1 but then error
except:
pass