Resizing tkinter listbox to width of largest item, using .grid Resizing tkinter listbox to width of largest item, using .grid tkinter tkinter

Resizing tkinter listbox to width of largest item, using .grid


You can list all your items to find the biggest (if there aren't too much it should be fine). For instance with strings you count their length with 'len(item)'.Then, when you create your listbox (not when you grid it) your set its width with 'width = "The size you want" ', if the size you put in there is well defined with regard to the length of the biggest item, you shouldn't have any problem.(I think I remember, the listbox's width's unity is given by the size of the text in it, but it needs to be checked)

I don't know grid that much, so that I don't know if there is any faster option to do it.

It should look something like this:

len_max = 0list_items = ["item2", "item2", "item3+a few characters for the size"]for m in list_items:    if len(m) > len_max:        len_max = len(m)import tkintermaster = Tk()my_listbox1 = Listbox(master, width = len_max)my_listbox1.grid(row = 0, column = 0)my_listbox2 = Listbox(master, width = len_max)my_listbox2.grid(row = 0, column = 1)my_listbox1.insert(END, list_items[0])my_listbox2.insert(END, list_items[1])my_listbox2.insert(END, list_items[2])master.mainloop()