How To Add A String That Contains Whitespace To A Tkinter Treeview Column
I use the tree.insert method to insert data into the treeview widget. Under the values option I specify the string variable where to get the data. If the string contains whitespace
Solution 1:
values
expects an iterable, so when you pass a string
to it, it will tend to do weird things. Easiest way to fix it in your code is to pass it as a tuple:
tree.insert("", 'end', item[0], text=item[0], values=(item[1],))
Solution 2:
Its not a big task just give a backslash before a white-space, like :-
data = [['John', '123456'], ['Mary', '123\ 456'], ['Edward', 'abcdef'], ['Lisa', 'ab\ cd\ ef']]
It will show you the white-spaces in your GUI because '123\ 456' will treated as a single element in type 'list'.
Solution 3:
Of the two answers given here, I tend to disagree with setting
values=item[1]
to values=(item[1])
since I still had an issue with Treeview splitting around spaces.
I tried the escaping solution and that worked perfectly. I'd take it a little further if I may and change your values=(item[1])
to values=(item[1].replace(" ", "\ "))
so your line would read::
tree.insert("", 'end', item[0], text=item[0], values=(item[1].replace(" ", "\ "))
...just as a catch all
Post a Comment for "How To Add A String That Contains Whitespace To A Tkinter Treeview Column"