2

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:

enter image description here

Table with 6 columns:

enter image description here

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.

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • you could create minimal working code with example columns in treeview - it would be simpler to test it. – furas Jun 05 '22 at 12:02
  • @furas I updated my post to show the issue – John Rey Vilbar Jun 05 '22 at 12:35
  • In the second picture there is no space for the scrollbar (no more red area), so to see it probably a horizontal scrollbar is needed to make it visible? This rises the question is scrolling still possible with more than 3 columns (arrow keys with mouse pointer over the window)? – Claudio Jun 05 '22 at 12:58
  • 1
    from: https://stackoverflow.com/questions/9561030/vertical-and-horizontal-scrollbars-on-tkinter-widget#9566381 : "A quick fix is to pack the vertical scrollbar first, then the horizontal, and then the canvas. Your "major" widget should always be one of the last things you pack/grid. ". I suppose this will solve the problem. Does it? – Claudio Jun 05 '22 at 13:01
  • 1
    @Claudio I tried your suggestion and thanks it actually worked. Solved the issue by packing the scrollbar first then the table. – John Rey Vilbar Jun 06 '22 at 05:24

0 Answers0