I'm trying to build a scrollable table in TK inter. I have been trying to follow some examples, mostly I am managing, but it seems that the child objects are causing the container frame to resize. I'm not exactly sure what I am doing wrong.
Basically I am drawing controls in a frame, contained in a canvas, which is scrollable with a scrollbar. This is then contained inside another frame. I would like the canvas + scrollbar to follow the parent frame shape / size.
from tkinter import *
# Table class
class Table:
# Initialize a constructor
def __init__(self, fmeHeaders, fmeScrollableData, Headings, Data):
total_rows = len(table_data)
total_columns = len(table_data[0])
for j in range(total_columns):
self.entry = Entry(fmeHeaders, width=15, bg='LightSteelBlue',fg='Black',font=('Arial', 16, 'bold'))
self.entry.grid(row=1, column=j)
self.entry.insert(END, Headings[j])
# An approach for creating the table
for i in range(total_rows):
for j in range(total_columns):
self.entry = Entry(fmeScrollableData, width=15, fg='blue', font=('Arial', 16, ''))
self.entry.grid(row=i, column=j)
self.entry.insert(END, Data[i][j])
table_header = ['ID', 'Name', 'City', 'Age']
# take the data
table_data = [(1, 'Gorge', 'California', 30),
(2, 'Maria', 'New York', 19),
(3, 'Albert', 'Berlin', 22),
(4, 'Harry', 'Chicago', 19),
(5, 'Vanessa', 'Boston', 31),
(6, 'Ali', 'Karachi', 32),
(7, 'John', 'Amsterdam', 23),
(8, 'Tom', 'Berlin', 15),
(9, 'Jane', 'Cape Town', 44),
(10, 'Hugo', 'Sidney', 17),
(11, 'Peter', 'Perth', 42),
(12, 'Philip', 'Toronto', 77)]
# create root window
root = Tk()
fmeTable = Frame(root, relief='sunken',borderwidth=2, height=2000, width=200)
canvas = Canvas(fmeTable)
scroll_y = Scrollbar(fmeTable, orient="vertical", command=canvas.yview)
fmeHeaders = Frame(fmeTable)
fmeHeaders.pack(side=TOP, anchor=NW)
fmeScrollableData = Frame(canvas)
table = Table(fmeHeaders, fmeScrollableData, table_header, table_data)
canvas.create_window(0, 0, anchor='nw', window=fmeScrollableData)
### make sure everything is displayed before configuring the scrollregion
canvas.update_idletasks()
canvas.configure(scrollregion=canvas.bbox('all'),
yscrollcommand=scroll_y.set)
canvas.pack(fill='both', expand=True, side='left')
scroll_y.pack(fill='y', side='right')
fmeTable.pack()
root.mainloop()