1

I have the following code to build a listbox with Treeview widget.

import tkinter as tk
import tkinter.ttk as ttk

class AppBase:
    def __init__(self):
        self.mywin = tk.Tk()
        self.mywin.geometry("%dx%d+%d+%d" % (800, 600, 5, 5))
        self.frame1 = tk.Frame(self.mywin)
        self.frame1.pack()

        lb_header = ['name', 'surname']
        lb_list = [
        ('John', 'Smith') ,
        ('Larry', 'Black') ,
        ('Walter', 'White') ,
        ('Fred', 'Becker') 
        ]

        self.tree = ttk.Treeview(columns=lb_header, show="headings")
        self.tree.grid(in_=self.frame1)

        for col in lb_header:
            self.tree.heading(col, text=col.title())
        for item in lb_list:
            self.tree.insert('', 'end', values=item)

    def start(self):
        self.mywin.mainloop()

app=AppBase()
app.start()  

It works but if there are few lines I have some empty lines at the end.

This is the result

If I increase the number of data lines I have less empty lines, I think there is a minimum number of lines and if the data lines are less than minimum the listbox shows white lines.

How can I fix that and set the listbox to have not minimun lines?

gaiapac
  • 109

1 Answers1

2

Those aren't empty lines, it's the height of the treeview widget.

I never worked with tkinter before, but looking at the docs I found the height option:

height  Specifies the number of rows which should be visible. Note: the requested width is determined from the sum of the column widths.

So you can specify the treeview height yourself, like this:

    self.tree = ttk.Treeview(columns=lb_header, show="headings", height=2)

Or to match the number of items:

    self.tree = ttk.Treeview(columns=lb_header, show="headings", height=len(lb_list))

Although I wouldn't recommend this as it will grow out of screen when a lot of items are added.

Timo
  • 4,700