I would like to insert the following strings into the treeview and display the complete string, however when I run the code, the string is not displayed completely in the treeview. I would also like to select the row in treeview to get back the string, the string is somewhat correct but is not the same as the initially inserted string. I am using Python 3.6. Below is the code I am using and some example outputs I observed. Kindly advise me on 2 things:
import tkinter as tk
import tkinter.ttk
selected_from_list = []
def select():
curItems = tree.selection()
for x in curItems:
print(x)
selected_from_list.append(tree.item(x)['values'])
print(selected_from_list)
print(selected_from_list)
print([str(tree.item(i)['values']) for i in curItems])
tk.Label(root, text="\n".join([str(tree.item(i)['values']) for i in curItems])).pack()
root = tk.Tk()
tree = tkinter.ttk.Treeview(root, height=4)
tree['show'] = 'headings'
tree['columns'] = ('File path')
tree.heading("#1", text='Badge Name', anchor='w')
tree.column("#1", stretch="no")
tree.pack()
tree.insert("", "end", values="C:/Users/PB/PyProjects/VA_PY_36/Python Excel project/people 1.xlsx")
tree.insert("", "end", values="C:/Users/PB/PyProjects/VA_PY_36/Python Excel project/people 2.xlsx")
tree.insert("", "end", values="C:/Users/PB/PyProjects/VA_PY_36/Python Excel project/people 3.xlsx")
tree.bind("<Return>", lambda e: select())
root.mainloop()
Thank you for sparing your time to guide a newbie coder! I would really appreciate your inputs!
I have tried to resize the treeview width but it does not seem to make a difference
Note that both columns
and values
options expect a tuple/list, but you passed a string to both of them. Seems like the string will be split by white spaces implicitly by the underlying TCL interpreter and you get what is shown in the posted image.
Pass correct types to the two options as below:
...
tree['columns'] = ('File path',) # changed to tuple by adding a comma before the ending )
...
tree.column("#1", stretch="no", width=400) # set larger width to show the content
...
# pass list to values option
tree.insert("", "end", values=["C:/Users/PB/PyProjects/VA_PY_36/Python Excel project/people 1.xlsx"])
tree.insert("", "end", values=["C:/Users/PB/PyProjects/VA_PY_36/Python Excel project/people 2.xlsx"])
tree.insert("", "end", values=["C:/Users/PB/PyProjects/VA_PY_36/Python Excel project/people 3.xlsx"])
...
Result: