Tkinter Ttk Treeview How to Set Fixed Width Why It Change With Number of Column

How to change ttk.Treeview column width and weight in Python 3.3

Treeview.Column does not have weight option, but you can set stretch option to False to prevent column resizing.

from tkinter import *
from tkinter.ttk import *


root = Tk()
tree = Treeview(root, selectmode="extended", columns=("A", "B"))
tree.pack(expand=YES, fill=BOTH)
tree.heading("#0", text="C/C++ compiler")
tree.column("#0", minwidth=0, width=100, stretch=NO)
tree.heading("A", text="A")
tree.column("A", minwidth=0, width=200, stretch=NO)
tree.heading("B", text="B")
tree.column("B", minwidth=0, width=300)
root.mainloop()

Python tkinter Treeview more columns than specified

Column "#0" always refers to the tree column, not for data columns. So use "#1", "#2", "#3" instead and set show="headings" to hide the tree column:

listbox = ttk.Treeview(
tab_player,
columns=('Player', 'Rating', 'Price'),
selectmode="extended",
show="headings" # hide the tree column
)

listbox.heading('#1', text='Player', anchor=tk.CENTER)
listbox.heading('#2', text='Rating', anchor=tk.CENTER)
listbox.heading('#3', text='Price', anchor=tk.CENTER)

listbox.column('#1', stretch=tk.YES, minwidth=50, width=80)
listbox.column('#2', stretch=tk.YES, minwidth=50, width=60)
listbox.column('#3', stretch=tk.YES, minwidth=50, width=60)

Automatic minimum width for column '#0' in tkinter.TreeView

On windows that expand / collapse button seems to be dynamically drawn based on the size, and according to this minwidth option defaults to 20. I'd write a method to calculate minwidth such that it accounts for the depth and image and text width + 20.

That being said, using this answer a method can be written for fixing the column width at the minwidth Tk defaults to, by breaking the tagbind at that exact location:

#the minimum width default that Tk assigns
minwidth = tree.column('#0', option='minwidth')

tree.column('#0', width=minwidth)

#disabling resizing for '#0' column particularly
def handle_click(event):
if tree.identify_region(event.x, event.y) == "separator":
if tree.identify_column(event.x) == '#0':
return "break"

#to have drag drop to have no effect
tree.bind('<Button-1>', handle_click)
#further disabling the double edged arrow display
tree.bind('<Motion>', handle_click)

And a complete example:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *
from tkinter import ttk

root = Tk()

tree = ttk.Treeview(root, columns=('one'))
idx = tree.insert(parent='', index=END, values=('AAA'))
tree.insert(parent=idx, index=END, values=('child'))

tree.column('#0', stretch=False) # NO effect!

#the minimum width default that Tk assigns
minwidth = tree.column('#0', option='minwidth')

tree.column('#0', width=minwidth)

#disabling resizing for '#0' column particularly
def handle_click(event):
if tree.identify_region(event.x, event.y) == "separator":
if tree.identify_column(event.x) == '#0':
return "break"

#to have drag drop to have no effect
tree.bind('<Button-1>', handle_click)
#further disabling the double edged arrow display
tree.bind('<Motion>', handle_click)

tree.pack()
root.mainloop()


Related Topics



Leave a reply



Submit