Search code examples
pythontkintertreeview

Tkinter - CheckboxTreeview height and horizontal scrolling


I have two questions on the python code at the bottom:

  1. I would like that the CheckboxTreeview widget covers as much height as there is available. If the user resizes the window, the should be more lines in the widget. I know about the height parameter, but this gives me always a fixed amount of rows. How can I modify the code to my needs?
  2. I would like the two columns x and y to have the fixed width as written in the code. If I resize the window, the width of these columns is also modified. Furthermore, is it somehow that the x-scrollbar is effective and can be used if the text in the first column is too long?

.

import tkinter as tk
from tkinter import ttk
from ttkwidgets import CheckboxTreeview

class App():
    def __init__(self, master):  
        self.master = master
        self.master.geometry('300x400')
        self.master.columnconfigure(0, weight=1)
        self.tree = CheckboxTreeview(self.master)
        xsb = ttk.Scrollbar(self.master, orient='horizontal', command=self.tree.xview)
        ysb = ttk.Scrollbar(self.master, orient='vertical', command=self.tree.yview)
        self.tree.configure(yscroll=ysb.set)
        self.tree['columns']=('x', 'y')
        self.tree.column('x', anchor='e', width=40)
        self.tree.column('y', width=40)
        self.tree.heading('#0', text='abc', anchor='w')
        self.tree.heading('x', text='x', anchor='w')
        self.tree.heading('y', text='y', anchor='w')
        self.tree.grid(row=0, column=0)
        ysb.grid(row=0, column=1, sticky='ns')
        xsb.grid(row=1, column=0, sticky='ew')
        self.master.frame = tk.Frame(self.master, highlightbackground="black", highlightthickness=2)#, background='#ff0')
        self.master.frame.grid(row=4, column=0, columnspan=2, sticky='w')
        tk.Label(self.master.frame, text='TEXT', font=tk.font.Font(size=16, weight='bold')).grid(row=0, column=0)
        self.tree.insert('', 'end', text='node 1', values=('x1', 'y1'), open=True)
        self.tree.insert('', 'end', text='node 2 - this is a very long text which should be scrollable', values=('x2', 'y2'), open=True)

root = tk.Tk()
app = App(root)
app.master.mainloop()

Solution

  • For item 1, you need to call self.master.rowconfigure(0, weight=1) and set sticky="nsew" in self.tree.grid(...).

    For item 2, add stretch=0 to self.tree.column('x', ...) and self.tree.column('y', ...)

    Updated code:

    import tkinter as tk
    from tkinter import ttk
    from ttkwidgets import CheckboxTreeview
    
    class App():
        def __init__(self, master):
            self.master = master
            self.master.geometry('300x400')
            self.master.rowconfigure(0, weight=1)  # added
            self.master.columnconfigure(0, weight=1)
            self.tree = CheckboxTreeview(self.master)
            xsb = ttk.Scrollbar(self.master, orient='horizontal', command=self.tree.xview)
            ysb = ttk.Scrollbar(self.master, orient='vertical', command=self.tree.yview)
            self.tree.configure(yscroll=ysb.set)
            self.tree['columns']=('x', 'y')
            self.tree.column('x', anchor='e', width=40, stretch=0) # added stretch=0
            self.tree.column('y', width=40, stretch=0) # added stretch=0
            self.tree.heading('#0', text='abc', anchor='w')
            self.tree.heading('x', text='x', anchor='w')
            self.tree.heading('y', text='y', anchor='w')
            self.tree.grid(row=0, column=0, sticky="nsew") # added sticky="nsew"
            ysb.grid(row=0, column=1, sticky='ns')
            xsb.grid(row=1, column=0, sticky='ew')
            self.master.frame = tk.Frame(self.master, highlightbackground="black", highlightthickness=2)#, background='#ff0')
            self.master.frame.grid(row=4, column=0, columnspan=2, sticky='w')
            tk.Label(self.master.frame, text='TEXT', font=tk.font.Font(size=16, weight='bold')).grid(row=0, column=0)
            self.tree.insert('', 'end', text='node 1', values=('x1', 'y1'), open=True)
            self.tree.insert('', 'end', text='node 2 - this is a very long text which should be scrollable', values=('x2', 'y2'), open=True)
    
    root = tk.Tk()
    app = App(root)
    app.master.mainloop()