0

I can't implement two working scrollbars for the Tree at the same time in this code.

import tkinter as tk
from tkinter import ttk
#python 3.9 , tkinter 8.6.9

root = tk.Tk()
root.title("Project Tree")
root.resizable(False, False)


# Создание Frame для дерева проекта и его надписи
frame = tk.Frame(root)
frame.pack(fill="both", expand=True, padx=10, pady=10)

# Создание Label над деревом
label = tk.Label(frame, text="Project Tree")
label.grid(row=0 , column=0, columnspan=1 , sticky="nsew")

# Создание виджета Treeview с горизонтальной и вертикальной полосами прокрутки
tree = ttk.Treeview(frame, columns=("type", "size"), show="tree")

# Настройка вертикальной полосы прокрутки
tree_scroll_v = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
tree.configure(yscrollcommand=tree_scroll_v.set)

# Настройка горизонтальной полосы прокрутки
tree_scroll_h = ttk.Scrollbar(frame, orient="horizontal", command=tree.xview)
tree.configure(xscrollcommand=tree_scroll_h.set)

# Размещение дерева и вертикальной полосы прокрутки на Frame
tree.grid(row=1 , column=0, sticky="nsew")
tree_scroll_v.grid(row=1 , column=1, sticky="nsew")

# Добавление элементов в дерево
project_node = tree.insert("", "end", text="Project", open=True)
src_node = tree.insert(project_node, "end", text="src", open=True)
py_node = tree.insert(src_node, "end", text="python", open=True)
tree.insert(py_node, "end", text=100*"main.py")
tree.insert(py_node, "end", text="utils.py")

docs_node = tree.insert(project_node, "end", text="docs", open=True)
tree.insert(docs_node, "end", text="README.md")
tree.insert(docs_node, "end", text=20*"LICENSE\n")

# Размещение горизонтальной полосы прокрутки под деревом и выравнивание по горизонтали
tree_scroll_h.grid(row=2 , column=0, sticky="nsew")
tree_scroll_h.config(command=tree.xview)
tree.configure(xscrollcommand=tree_scroll_h.set)

root.mainloop()

I asked ChatGPT how to implement this, but either I posed the question incorrectly, or he doesn't know either. I tried to implement how to add "wrap=NONE" to the tree in "Text", but it does not accept such parameters.

  • Set the `width` of column `"#0"` (tree column) to a large value and `stretch=0` after the window is shown (you can use `.after()` to do it). – acw1668 Mar 24 '23 at 16:15
  • 1
    It doesn't look like you've done any research. There are other questions and answers (such as [this](https://stackoverflow.com/a/14403393/7432) and [this](https://stackoverflow.com/questions/49715456/forcing-a-tkinter-ttk-treeview-widget-to-resize-after-shrinking-its-column-width)). You should start by searching this site for similar questions. – Bryan Oakley Mar 24 '23 at 16:43
  • thanks for the advice and provided similar questions. – Alex Ovanov Mar 24 '23 at 16:58
  • Does this answer will help? [https://stackoverflow.com/questions/14359906/horizontal-scrolling-wont-activate-for-ttk-treeview-widget][1] [1]: https://horizontal – toyota Supra Mar 25 '23 at 08:53

0 Answers0