Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TKinter ListBox item height

Tags:

python

tkinter

Is there a way to shrink the height of the cells of TKinter listbox at initialization?

self.lb = Listbox(f,selectmode=MULTIPLE, bd=1, height=10)
self.lb.bind("<<ListboxSelect>>", self.onSelectlbItem)
self.lb.grid(row=3, column=1,columnspan=7, sticky="WE", pady=0)
like image 549
user1529412 Avatar asked Sep 13 '25 21:09

user1529412


2 Answers

The height of the rows in a tkinter listbox is dependent on the size of the text font. This can be set for the whole widget (although not for individual rows), either as the font= optional argument at initialization, or using the .config method of the listbox post-initiatlization.

Below is how the code might look if you do the font-size setting at initialization time:

import tkFont
small_font = tkFont.Font(size=5) # Specify font size, and use default style for other parameters

self.lb = Listbox(f,selectmode=MULTIPLE, bd=1, height=10, font=small_font)

Edit The above example is for Python 2. If you are using Python 3, I believe the correct way to import/use the font utilities is now from tk import font and small_font = font.Font(...).

like image 54
zehnpaard Avatar answered Sep 17 '25 21:09

zehnpaard


The height of the rows do depend on the font size, but there is an easier way to change the font size without the need to import anything:

self.lb = Listbox(f,selectmode=MULTIPLE, bd=1, height=10, font=('Times', 14))
like image 38
Mohamed Adem Avatar answered Sep 17 '25 21:09

Mohamed Adem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!