How to insert tabs \t in a listbox? How to insert tabs \t in a listbox? tkinter tkinter

How to insert tabs \t in a listbox?


The way to insert a tab in a listbox is exactly how you are doing it. You just happen to be inserting data that aligns with the tabstops. To see what I mean, add the following line to your code:

Lb.insert("end", "1\t2\t3\t4\t5")

You will see that the date and the word "male" line up exactly with some of the tabstops. Unfortunately you cannot configure the tabstops in a listbox.

enter image description here

If you need configurable tabstops, you'll have to simulate a listbox using a Text widget with some custom bindings.


After research, I have learned that if you want to insert a tab \t into a tkinter listbox, it will not work on a Windows OS, I do not know why...

I have managed to find a solution created previosuly by wiffleball687 from www.reddit.com.

import tkinterdef tabify(s, tabsize = 4):    ln = ((len(s)/tabsize)+1)*tabsize    return s.ljust(ln)rt = tkinter.Tk()listbox = tkinter.Listbox(rt, font = 'Courier') #change to a fixed width fontlistbox.pack()listbox.insert(tkinter.END, tabify('a') + 'b')rt.mainloop()


I do not think that there is any way to have this functionality in your Tkinter GUIs for now.

After some research I have found a way to format text in a GUI using a Text widget.

However, I was unsuccessful in an attempt to use this widget in a Listbox.

Here is a simple sample of code to demo the use of a Text widget.

from tkinter import *import tkinterroot = Tk()name = 'Jeff'dob = '01/01/2000'gender = 'male'text = Text(root, height = 2, width = 30)text.insert(END, str(name)+'\t'+str(dob)+'\t'+str(gender))text.pack()root.mainloop()

When I tried to insert the widget Text into a Listbox like you have above in your code I got this result.

Attempt to use a Text widget in a Listbox using Tkinter

I hope that this was helpful and if you have any further questions please feel free to post a comment below!