Reputation: 55
I'm trying to get a CheckboxTreeview to display in a frame with both x and y scrollbars using the information from the blog post here https://blog.tecladocode.com/tkinter-scrollable-frames/ about using a canvas to create the scrollbars.
import tkinter as tk
from tkinter import ttk
from ttkwidgets import CheckboxTreeview
import sqlite3
INSERT_TRAIT = "INSERT into evaluation_trait_table (trait_name, trait_type, evaluation_trait_display_order) values (?, ?, ?);"
def add_trait(connection, trait_name, trait_type, evaluation_trait_display_order):
with connection:
connection.execute(INSERT_TRAIT, (trait_name, trait_type, evaluation_trait_display_order))
class ScrollableFrame(ttk.Frame):
def __init__(self, container, *args, **kwargs):
super().__init__(container, *args, **kwargs)
canvas = tk.Canvas(self, width=500, height=300)
yscrollbar = ttk.Scrollbar(self, orient="vertical", command=canvas.yview)
xscrollbar = ttk.Scrollbar(self, orient="horizontal", command=canvas.xview)
self.scrollable_frame = ttk.Frame(canvas)
self.scrollable_frame.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=yscrollbar.set)
canvas.configure(xscrollcommand=xscrollbar.set)
yscrollbar.pack(side="right", fill="y")
xscrollbar.pack(side="bottom", fill="x")
canvas.pack(side="left", fill="both", expand=True)
root = tk.Tk()
root.columnconfigure(0, weight=1)
root.rowconfigure(1, weight=1)
connection = sqlite3.connect('treeview_test_db.sqlite')
cmd = "CREATE TABLE if not exists evaluation_trait_table (id INTEGER PRIMARY KEY ,trait_name CHAR NOT NULL " \
",trait_type INTEGER,evaluation_trait_display_order INTEGER DEFAULT (null) ) "
connection.execute(cmd)
add_trait(connection,'Teeth Alignment', 1, 38)
add_trait(connection,'Missing Teeth', 1, 31)
add_trait(connection,'White on Nose', 1, 44)
add_trait(connection,'Head', 1, 21)
add_trait(connection,'Horn Shape', 1, 24)
add_trait(connection,'Body', 1, 3)
add_trait(connection,'Legs', 1, 27)
add_trait(connection,'White in Britch or Body', 1, 43)
add_trait(connection,'White on Nose', 1, 44)
add_trait(connection,'Udder Lumps', 1, 40)
add_trait(connection,'Fleece Quality', 1, 18)
add_trait(connection,'Fleece Soft', 1, 19)
add_trait(connection,'Fleece Length', 2, 17)
add_trait(connection,'Fleece Weight', 2, 20)
add_trait(connection,'Scrotal Circumference', 2, 35)
add_trait(connection,'Weight', 2, 42)
add_trait(connection,'Horn Buds', 1, 23)
add_trait(connection,'Temperament', 1, 39)
add_trait(connection,'Fleece Crimp', 1, 16)
add_trait(connection,'Insemination Depth', 2, 25)
my_frame = ScrollableFrame(root)
tree = CheckboxTreeview(my_frame.scrollable_frame
, column=("column1", "column2", "column3", "column4"), show=("headings", "tree"))
tree.heading("#0", text="Select")
tree.column("#0", minwidth=50, width=50)
tree.heading("#1", text="id_traitid")
tree.column("#1", minwidth=75, width=75)
tree.heading("#2", text="Trait Name")
tree.column("#2", minwidth=150, width=150)
tree.heading("#3", text="trait_type")
tree.column("#3", minwidth=150, width=150)
tree.heading("#4", text="evaluation_trait_display_order")
tree.column("#4", minwidth=150, width=150)
connection = sqlite3.connect('treeview_test_db.sqlite')
cursor = connection.cursor()
cmd = "select * from evaluation_trait_table"
cursor.execute(cmd)
rows = cursor.fetchall()
for row in rows:
print(row)
tree.insert('', 'end', values=row)
tree.pack()
my_frame.grid(row=0, column=0)
my_header_label = tk.Label(root, text='The frame below works properly with 2 scrollbars and fills the blank space in the canvas.', bg="white")
my_header_label.grid(row=1, column=0)
frame2 = ScrollableFrame(root)
frame2.grid(row=2, column=0)
for i in range(50):
ttk.Label(frame2.scrollable_frame, text="Sample scrolling label that is really long to "
"test if my 2 way scrolling works", width=900).pack()
frame3 = ScrollableFrame(root)
frame3.grid(row=2, column=1)
for i in range(50):
ttk.Label(frame3.scrollable_frame, text="This is a second scrolling frame that I have gridded "
"elsewhere. It is also a very long line to verify that the horixntal scrolling works.").pack()
root.mainloop()
The first problem is that the vertical scrollbar does not display on the first frame. However positioning the mouse in the frame I can scroll up and down using a mouse scroll wheel.
Second problem is that the treeview does not fill the frame vertically whereas the frames made with a label in a loop do fill and scroll properly.
Does anyone have any ideas on why the treeview won't work and how to fix it?
Upvotes: 0
Views: 324
Reputation: 46786
It is because the default value for the height
option of the treeview is 10, so only 10 rows will be shown. Set the height
option to the number of rows after inserting all the data into it:
...
rows = cursor.fetchall()
for row in rows:
print(row)
tree.insert('', 'end', values=row)
tree.config(height=len(rows))
tree.pack()
...
Upvotes: 1