How to view partial area of a treeview and horizontal scrollable in tkinter?
I have a table created by tk.treeview, for example, there're 10 columns. The target is to build a treeview widget with horizontal scrollbar, the view width of widget is set just about 4 columns and use horizontal scrollbar to view other columns.
Using tk.Frame as parent widget and with scrollbar, vertical scroll will hide the header, so not use it.
Platform: WIN 10 Python: 3.8.6 tkinter: 8.6
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# root.columnconfigure(0, weight=1)
# root.rowconfigure(0, weight=1)
columns = [f'Column {i}' for i in range(10)]
x_scrollbar = tk.Scrollbar(root, orient=tk.HORIZONTAL)
x_scrollbar.grid(row=1, column=0, sticky=tk.E+tk.W)
y_scrollbar = tk.Scrollbar(root, orient=tk.VERTICAL)
y_scrollbar.grid(row=0, column=1, sticky=tk.N+tk.S)
tree = ttk.Treeview(root, columns=columns, height=10, show="headings",
xscrollcommand=x_scrollbar.set, yscrollcommand=y_scrollbar.set)
tree.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)
for col in tree['columns']:
tree.heading(col, text=f"{col}", anchor=tk.CENTER)
tree.column(col, anchor=tk.CENTER, width=100)
for i in range(100):
tree.insert('', 'end', values=[i*10+j for j in range(len(columns))])
x_scrollbar['command'] = tree.xview
y_scrollbar['command'] = tree.yview
root.mainloop()
The question is how can I set the view width just about 4 columns and with horizontal scrollbar work ? like this ! (no windiw resize)
Solution 1:
There is a tricky way to do it:
- initially set the column widths to a smaller size, for your case it is 40 (i.e. 400 / 10)
- force update the treeview using
tree.update()
- set the column widths to the desired size, i.e. 100
for col in tree['columns']:
tree.heading(col, text=f"{col}", anchor=tk.CENTER)
tree.column(col, anchor=tk.CENTER, width=40) # initially smaller size
tree.update()
for col in tree['columns']:
tree.column(col, width=100) # restore to desired size