Bug report
import tkinter as tk
from tkinter import ttk
def center_window(window: tk.Tk | tk.Toplevel, width: int, height: int) -> None:
window.update_idletasks()
x = (window.winfo_screenwidth() // 2) - (width // 2)
y = (window.winfo_screenheight() // 2) - (height // 2)
window.geometry(f"{width}x{height}+{x}+{y}")
def configure_styles() -> None:
style = ttk.Style()
# configuration with kwargs causes a forced redraw of the entire application
# which ends up recalculating the width of the treeview in root on
# subsequent calls.
style.configure("Treeview", rowheight=40, font=("Segoe UI Semibold", 18))
def create_root_window() -> tk.Tk:
root = tk.Tk()
root.title("Treeview Bug Example")
center_window(root, 800, 800)
base_frame = ttk.Frame(root)
base_frame.grid(row=0, column=0, sticky="nsew")
base_frame.rowconfigure(0, weight=1)
base_frame.columnconfigure(0, weight=1)
base_frame.columnconfigure(1, weight=1)
f1 = ttk.Frame(base_frame)
f1.grid(row=0, column=0, sticky="nsew")
f1.rowconfigure(0, weight=1)
f1.columnconfigure(0, weight=1)
tree = ttk.Treeview(f1, show="tree", selectmode="browse")
# tree.column("#0", width=300, stretch=False)
tree.grid(row=0, column=0, sticky="nsew")
scrollbar = ttk.Scrollbar(f1, orient="vertical", command=tree.yview)
scrollbar.grid(row=0, column=1, sticky="ns")
tree.configure(yscrollcommand=scrollbar.set)
tree.insert("", "end", text="Item 1")
tree.insert("", "end", text="A longer string for Item 2")
tree.insert("", "end", text="Another item")
f2 = ttk.Frame(base_frame)
f2.grid(row=0, column=1, sticky="nsew")
run_btn = ttk.Button(f2, text="Run", command=configure_styles)
run_btn.grid(row=0, column=0, sticky="nsew")
exit_btn = ttk.Button(f2, text="Exit", command=root.destroy)
exit_btn.grid(row=1, column=0, sticky="nsew")
configure_styles()
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.bind("<Escape>", lambda event: root.destroy())
return root
def main() -> None:
root = create_root_window()
root.mainloop()
if __name__ == "__main__":
main()
I'm running Python 3.14.3 and Tcl version: 8.6.15 on Windows 11
Following the code example above, when the Style() object singleton is edited multiple times, the width of the root Treeview is recalculated, shifting the buttons on the root window to the right. This width change does not reset at any point, and in fact will continue to get worse each time Style() is recreated and modified. A workaround is to calculate the width you need and set the width internally to that calculated value, a simple version of which is commented out in my example above.
# tree.column("#0", width=300, stretch=False)
Uncommenting that line prevents the column #0 from stretching when modifying Style() over and over again, however, having the width be recalculated like this every single time Style() is recreated seems like unintended behavior as Style() is supposed to be a singleton and configuring it, while it forces a redraw, should not continuously expand the width of the Treeview if the values within the Treeview itself are not changing.
CPython versions tested on:
3.14
Operating systems tested on:
Windows
Bug report
I'm running Python 3.14.3 and Tcl version: 8.6.15 on Windows 11
Following the code example above, when the Style() object singleton is edited multiple times, the width of the root Treeview is recalculated, shifting the buttons on the root window to the right. This width change does not reset at any point, and in fact will continue to get worse each time Style() is recreated and modified. A workaround is to calculate the width you need and set the width internally to that calculated value, a simple version of which is commented out in my example above.
# tree.column("#0", width=300, stretch=False)Uncommenting that line prevents the column #0 from stretching when modifying Style() over and over again, however, having the width be recalculated like this every single time Style() is recreated seems like unintended behavior as Style() is supposed to be a singleton and configuring it, while it forces a redraw, should not continuously expand the width of the Treeview if the values within the Treeview itself are not changing.
CPython versions tested on:
3.14
Operating systems tested on:
Windows