How to pick up a Tkinter list for content

I am adding lines to the list using the code below. When I run the code and the window opens, longer lines are cut off, since the window is not large enough (see screenshot). I tried resizing the window and adding scrollbars, but I was wondering if there was a way to automatically resize it to fit the content.

master = tk.Tk() listbox = tk.Listbox(master, selectmode=tk.SINGLE) games = ["Garry Mod", "Mount and Blade: Warband", "Tekkit"] for game in sorted(games): listbox.insert(tk.END, game) button = tk.Button(master, text="Execute", command=execute) listbox.pack() button.pack() tk.mainloop() 

example of clipped listbox

+5
source share
4 answers

Resetting the width of the list worked for me. I used Oblivion's answer and noticed that the width is always zero.

 listbox = tk.Listbox(master, selectmode=tk.SINGLE) listbox.config(width=0) 

I also recommend resetting the root window geometry after reloading the contents of the list. Otherwise, if the user manually expands the window, the window ceases to adapt to the size of its contents.

 root.winfo_toplevel().wm_geometry("") 
+10
source

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() 
+3
source

tkListAutoWidth.py shows one way to do this:

http://svn.effbot.org/public/stuff/sandbox/tkinter/

edit:

So you may have something similar,

 import tkinter as tk from tkinter import font class NewListbox(tk.Listbox): def autowidth(self, maxwidth=100) autowidth(self, maxwidth) def autowidth(list, maxwidth=100): f = font.Font(font=list.cget("font")) pixels = 0 for item in list.get(0, "end"): pixels = max(pixels, f.measure(item)) # bump listbox size until all entries fit pixels = pixels + 10 width = int(list.cget("width")) for w in range(0, maxwidth+1, 5): if list.winfo_reqwidth() >= pixels: break list.config(width=width+w) if __name__ == "__main__": master = tk.Tk() listbox = NewListbox(master, selectmode=tk.SINGLE) # ... # ... keys = serverDict.keys() for key in sorted(keys): listbox.insert("end", key) listbox.pack() button = tk.Button(master, text="Execute", command=execute) button.pack() listbox.autowidth() master.mainloop() 
+2
source

just specify width and height 0 below

 listbox.config(width=0,height=0) 
+1
source

All Articles