I have issue dealing with scrollbar in tkinter.
I enclosed Treeview
and Scrollbar
widgets inside a frame for easier grouping. The problem is that when I update the Treeview
to display a table with many columns, the scroll bar suddenly disappears (scroll bar retains only when at most 3 columns are displayed).
window = tk.Tk()
window.minsize(800, 700)
window.geometry("800x700")
...
middle = tk.Frame(window)
middle.pack(fill='x', side='top')
#red background for visualizing only
middle.configure(background='red')
displayTable = ttk.Treeview(middle, show='headings', selectmode="browse")
displayTable.pack(side=tk.LEFT)
#style for the table
style = ttk.Style()
style.theme_use("clam")
style.map("Treeview")
style.configure("Treeview.Heading", font=("Consolas", 12))
style.configure("Treeview", rowheight=30)
tabScroll = ttk.Scrollbar(middle, orient=tk.VERTICAL, command=displayTable.yview)
displayTable.configure(yscrollcommand=tabScroll.set)
tabScroll.pack(side=tk.RIGHT, fill='y')
I want that the scroll bar widget will not disappear whenever I display tables with cols > 3. Any idea where is the issue? I dont want to use grid()
since the main window is resizable.
Table with 2 columns:
Table with 6 columns:
Note: data in table are fictitious
EDIT: as requested, below are the two variants for the working example:Table with 2 rows:
import tkinter as tk from tkinter import ttk window = tk.Tk() window.minsize(800, 700) window.geometry("800x700") middle = tk.Frame(window) middle.pack(fill='x', side='top') #red background for visualizing only middle.configure(background='red') displayTable = ttk.Treeview(middle, show='headings', selectmode="browse") displayTable.pack(side=tk.LEFT) #headings columns = ("id", "name") displayTable['column'] = columns displayTable.heading("# 1",text="id") displayTable.heading("# 2",text="name") #rows for n in range(1, 100): displayTable.insert('', tk.END, values=(n, f"Row entry{n}")) #style for the table style = ttk.Style() style.theme_use("clam") style.map("Treeview") style.configure("Treeview.Heading", font=("Consolas", 12)) style.configure("Treeview", rowheight=30) tabScroll = ttk.Scrollbar(middle, orient=tk.VERTICAL, command=displayTable.yview) displayTable.configure(yscrollcommand=tabScroll.set) tabScroll.pack(side=tk.RIGHT, fill='y') window.mainloop()
Table with 4 rows:
import tkinter as tk from tkinter import ttk window = tk.Tk() window.minsize(800, 700) window.geometry("800x700") middle = tk.Frame(window) middle.pack(fill='x', side='top') #red background for visualizing only middle.configure(background='red') displayTable = ttk.Treeview(middle, show='headings', selectmode="browse") displayTable.pack(side=tk.LEFT) #headings columns = ("id", "name", "job", "number") displayTable['column'] = columns displayTable.heading("# 1",text="id") displayTable.heading("# 2",text="name") displayTable.heading("# 3",text="job") displayTable.heading("# 4",text="number") #rows for n in range(1, 100): displayTable.insert('', tk.END, values=(n, f"Row entry{n}", f"Job{n}", f"09-{n}")) #style for the table style = ttk.Style() style.theme_use("clam") style.map("Treeview") style.configure("Treeview.Heading", font=("Consolas", 12)) style.configure("Treeview", rowheight=30) tabScroll = ttk.Scrollbar(middle, orient=tk.VERTICAL, command=displayTable.yview) displayTable.configure(yscrollcommand=tabScroll.set) tabScroll.pack(side=tk.RIGHT, fill='y') window.mainloop()
My goal is that to not suppress the scrollbar when the table displays more than 3 columns (in that specific window dimensions). Hope this helps.