0

I'm trying to get my Treeview to fit within the right-side frame, but I'm not sure where in my code is causing the frame to fall outside the window. The Treeview has 20+ columns and both horizontal/vertical scrollbars assigned to the frame. I also noticed that when I populate the Treeview with rows, I'm able to scroll up and down but I can't use the horizontal scrollbar, could this have something to do with it?

Thank you all.

Tkinter Window

from tkinter import *
from tkinter import filedialog
from tkinter import ttk
import os

root = Tk()
root.geometry("800x600")
root.resizable(False,False)

commandFrame = Frame(root) # Define frame for commands
commandFrame.grid(row=0,column=0,pady=20,padx=(10,0),sticky=N) # Place frame on left side

dirButton = Button(commandFrame, text="Select Database", width=20, pady=5, bd=3)
dirButton.grid(row=0,column=0,pady=(0,3))

saveButton = Button(commandFrame, text="Save Results", state=DISABLED, width=20, pady=5, bd=3)
saveButton.grid(row=1,column=0,pady=(0,3))

resetButton = Button(commandFrame, text="Reset", state=DISABLED, width=20, pady=5, bd=3)
resetButton.grid(row=2,column=0,pady=(0,3))

helpButton = Button(commandFrame, text="Help", width=20, pady=5, bd=3)
helpButton.grid(row=3,column=0,pady=(0,3))

dbButton = Button(commandFrame, text="Create Database", width=20, pady=5, bd=3)
dbButton.grid(row=4,column=0,pady=(0,3))

updateButton = Button(commandFrame, text="Update Database", state=DISABLED, width=20, pady=5, bd=3)
updateButton.grid(row=5,column=0,pady=(0,3))

tableFrame = Frame(root)                                # Define frame for query area
tableFrame.grid(row=0,column=1,pady=20,padx=10)         # Place frame on right side

yscroll = Scrollbar(tableFrame, orient="vertical")      # Define scroll bars for query area
xscroll = Scrollbar(tableFrame, orient="horizontal")
yscroll.pack(side=RIGHT, fill=Y)                        # Pack scroll bars for query area
xscroll.pack(side=BOTTOM, fill=X)

tableArea = ttk.Treeview(tableFrame, yscrollcommand=yscroll.set, 
                         xscrollcommand=xscroll.set, height=10) # Define Treeview with scrollbars
yscroll.config(command=tableArea.yview)                         # Assign scrollbar to axes
xscroll.config(command=tableArea.xview)
tableArea.pack()                                                # Put in frame

tableArea["columns"] = ("1","2","3","4","5","6","7","8","9","10","11","12",
                        "13","14","15","16","17")

# Format columns
tableArea.column("#0", width=0, stretch=NO)
tableArea.column("1", width=120, minwidth=25, anchor=W)
tableArea.column("2", width=120, minwidth=25, anchor=W)
tableArea.column("3", width=120, minwidth=25, anchor=W)
tableArea.column("4", width=120, minwidth=25, anchor=W)
tableArea.column("5", width=120, minwidth=25, anchor=W)
tableArea.column("6", width=120, minwidth=25, anchor=W)
tableArea.column("7", width=120, minwidth=25, anchor=W)
tableArea.column("8", width=120, minwidth=25, anchor=W)
tableArea.column("9", width=120, minwidth=25, anchor=W)
tableArea.column("10", width=120, minwidth=25, anchor=W)
tableArea.column("11", width=120, minwidth=25, anchor=W)
tableArea.column("12", width=120, minwidth=25, anchor=W)
tableArea.column("13", width=120, minwidth=25, anchor=W)
tableArea.column("14", width=120, minwidth=25, anchor=W)
tableArea.column("15", width=120, minwidth=25, anchor=W)
tableArea.column("16", width=120, minwidth=25, anchor=W)
tableArea.column("17", width=120, minwidth=25, anchor=W)

root.mainloop()
Buracku
  • 37
  • 6
  • Please create a [mre] – Thingamabobs Nov 16 '21 at 23:14
  • @Thingamabobs Alright I believe I did so, all the code to reproduce the window is updated above – Buracku Nov 16 '21 at 23:25
  • if I run your code I did not get the output of your picture. – Thingamabobs Nov 16 '21 at 23:28
  • @Thingamabobs Sorry for the mixup, forgot to copy over the columns and mainloop. There were some items I was removing and forgot to copy those over, it should be good now. – Buracku Nov 16 '21 at 23:37
  • You need to make sure that your frame isnt expanding to trigger the desired behavior. You can achieve this by writing after `tableArea.pack()` these two lines `root.update_idletasks()` and `tableFrame.pack_propagate(0)`. Another way can be to use a canvas instead of a frame for this. – Thingamabobs Nov 16 '21 at 23:54
  • @Thingamabobs Thank you for the reply sir, your suggestion worked albeit the treeview shrunk to about half the size of the frame view, although it did keep it from falling off the screen. I'll do some research to see if I can fit it within the frame and I'll add an answer if I come up with anything. – Buracku Nov 17 '21 at 00:08
  • You can specify the dimensons of the frame befor calling propagat(0), that would avoid calling idletasks() too. I did wrote a overview for tkinters geometry managment [here](https://stackoverflow.com/questions/63536505/how-do-i-organize-my-tkinter-appllication/63536506#63536506), if you intrested. – Thingamabobs Nov 17 '21 at 00:10
  • You can first set those columns to a smaller widths so that the total width is the width you want. Then call `tableArea.update()` and set the widths again to the desired sizes. – acw1668 Nov 17 '21 at 00:15
  • You can reference my answer to this [question](https://stackoverflow.com/questions/64477453/how-to-view-partial-area-of-a-treeview-and-horizontal-scrollable-in-tkinter). – acw1668 Nov 17 '21 at 01:00

0 Answers0