There are multiple ways to add columns.
Using a for-loop:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tree = ttk.Treeview(root,columns=())
tree.pack(fill='x')
for i in range(5):
param = tree['columns']+(f'#{i+1}',)
tree.configure(columns=param)
for i in range(5):
tree.heading(column=f'#{i+1}',text=f'text{i+1}',anchor='w')
tree.column(column=f'#{i+1}', width=150,minwidth=50,stretch=False)
root.mainloop()
Using a list:
import tkinter as tk
from tkinter import ttk
cols = ['one','two','three','four','five']
root = tk.Tk()
tree = ttk.Treeview(root,columns=cols)
tree.pack(fill='x')
for i in cols:
tree.heading(column=f'{i}',text=f'{i}',anchor='w')
tree.column(column=f'{i}', width=150,minwidth=50,stretch=False)
root.mainloop()
manually:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tree = ttk.Treeview(root,columns=('one','two','three','four','five'))
tree.pack(fill='x')
for i in tree['columns']:
tree.heading(column=f'{i}',text=f'{i}',anchor='w')
tree.column(column=f'{i}', width=150,minwidth=50,stretch=False)
root.mainloop()
generic:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tree = ttk.Treeview(root,columns=tuple(f"{i}" for i in range(5)))
tree.pack(fill='x')
for i in tree['columns']:
tree.heading(column=f'{i}',text=f'{i}',anchor='w')
tree.column(column=f'{i}', width=150,minwidth=50,stretch=False)
root.mainloop()