Following code should show a list of numbers in a treeview, and the column width of the TreeView should always remain fixed and it should not expand at all.
If an element does not fit inside the treeveiw the horizontal scrollbar under the Treeview should get activated and it should let the user to slide the contents of the column to either of the left or right sides.
at the same time the progressbar under the scrollbar should show the progression percentage including the percentage text in the middle of the progressbar and it should work fine without causing other components of the GUI window to expand or change in size.
To summarize following piece of code has two issues that I gave up trying different solutions available online,
(1) The horizontal scrollbar does not work properly, I have tried different combinations for width, minwidth, stretch for the scrollbar to work but none of them worked.
(2) When I use the stretch=tk.YES for the treeview in order to make the scrollbar to work, the update of the progressbar label (Text) forces the treeview to expands for no reason.
Any help for solving both issues together is really appreciated. I am using Python 3.8.6(64-bit)/Tkinter 8.6.9 on Win10 machine.
import tkinter as tk
from tkinter import ttk
class TreeviewTest(ttk.Frame):
def __init__(self, container, controller):
super().__init__(container)
self.list_num = ttk.Treeview(self, columns=("N"), show="tree headings",
height=20, selectmode="none")
self.list_num.heading(text="#", column=0, anchor=tk.W)
self.list_num["show"] = "headings"
self.list_num.column("#1",width=30 , minwidth=35, stretch=tk.YES)
self.refresh_csv_list()
self.list_num.grid(row=0, column=0, sticky="ew")
self.text = tk.Label(self, text="Sometext")
self.text.grid(row=0, column=1)
self.value = tk.DoubleVar()
self.style = ttk.Style()
self.style.layout("text.Horizontal.TProgressbar",
[("Horizontal.Progressbar.trough",
{"children": [("Horizontal.Progressbar.pbar",
{"side": "left", "sticky": "ns"})],
"sticky": "nswe"}),
("Horizontal.Progressbar.label", {"sticky": ""})])
self.h_list_num_scrollbar = ttk.Scrollbar(self, orient=tk.HORIZONTAL, command=self.list_num.xview)
self.h_list_num_scrollbar.grid(row=1, column=0, sticky="ew", columnspan=1)
self.list_num.configure(xscrollcommand = self.h_list_num_scrollbar.set)
self.pbar = ttk.Progressbar(self, orient="horizontal", length=300, maximum =100,
variable=self.value, style="text.Horizontal.TProgressbar")
self.pbar.grid(row=2, column=0, sticky="EW", pady=8, padx=3, columnspan=3)
self.update_progressbar(reset=True)
self.increment()
def increment(self):
self.update_progressbar(amount = 0.5)
self.after(20, self.increment)
def update_progressbar(self, amount=0, reset=False):
if reset:
new_value = 0
else:
new_value = self.value.get() + amount
if new_value < 0:
new_value = 0
if new_value > 100:
new_value = 100
self.value.set(new_value)
self.style.configure(style="text.Horizontal.TProgressbar",
text="{:.1f} %".format(self.value.get()))
def refresh_csv_list(self):
for i in range(50001000,50009000):
tg_num = f"tg_n__{i}"
self.list_num.insert("",i, text="",values=(str(i),) , tags=(tg_num,))
class MainPage(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.minsize(250,250)
self.title("Isolated GUI")
self.frames = dict()
container = ttk.Frame(self)
container.grid(padx=10, pady=10)
container.columnconfigure(0, weight=1)
frame_tree_view = TreeviewTest(container, self)
self.frames[TreeviewTest] = frame_tree_view
frame_tree_view.grid(row=0, column=0, sticky="NSEW")
self.show_frame(TreeviewTest)
def show_frame(self, container):
frame = self.frames[container]
frame.tkraise()
root = MainPage()
root.mainloop()