This is based on Oblivion's answer, but I edited it to work for me.
I created a new list class that was based on the original, but had a new function that I got from Oblivion code. Then I call this function, and it makes the corresponding list appropriate.
class Listbox(tk.Listbox): def autowidth(self,maxwidth): f = font.Font(font=self.cget("font")) pixels = 0 for item in self.get(0, "end"): pixels = max(pixels, f.measure(item)) # bump listbox size until all entries fit pixels = pixels + 10 width = int(self.cget("width")) for w in range(0, maxwidth+1, 5): if self.winfo_reqwidth() >= pixels: break self.config(width=width+w) master = tk.Tk() listbox = Listbox(master, selectmode=tk.SINGLE) keys = serverDict.keys() for key in sorted(keys): listbox.insert(tk.END, key) button = tk.Button(master, text="Execute", command=execute) listbox.autowidth(250) listbox.pack() button.pack() tk.mainloop()
source share