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
How to Locate Elements on Webpage With Headless Chrome
How to Write a Lambda Function That Is Conditional on Two Variables (Columns) in Python
Printing Even Characters With Strings in Python
Python Not Working in the Command Line of Git Bash
Truth Value of a Series Is Ambiguous. Use A.Empty, A.Bool(), A.Item(), A.Any() or A.All()
Merge Multiple Rows to Single Row - Pandas
Unit Testing a Method With No Return Value
Send a File Through Sockets in Python
Drop Non-Numeric Columns from a Pandas Dataframe
Dice Rolling Simulator in Python
Pip Installation /Usr/Local/Opt/Python/Bin/Python2.7: Bad Interpreter: No Such File or Directory
Find the Longest Substring in Alphabetical Order
How to Append Two Bytes in Python
Django - How to Retrieve Data in Database in Dropdownlist
Asking the User for Input Until They Give a Valid Response
How to Send Smtp Email for Office365 With Python Using Tls/Ssl
Python/Pandas: How to Match List of Strings With a Dataframe Column