Based on the example from here: Scrollbar in Tkinter grid
I made a simplified version which is more or less what i need except i would like the columns to always fill the width of the frame as the window is beeing resized.
Without the scrollbar it was super easy, i just added grid_columnconfigure
and that worked out of the box, but when I added the scrollbar i couldn't figure out how to get the columns to resize again.
Here is the example:
import tkinter as tk
row = 1
class ProgramWindow(tk.Frame):
def __init__(self):
self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
tk.Frame.__init__(self, self.canvas)
self.grid(column=0, row=0, sticky='ESW')
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
self.grid_columnconfigure(2, weight=1)
tk.Label(self, text="FirstCol", ).grid(row=0, column=0)
tk.Label(self, text="SecndCol", ).grid(row=0, column=1)
tk.Label(self, text="ThirdCol", ).grid(row=0, column=3)
self.vsb = tk.Scrollbar(root, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.canvas.pack(side="left", fill="both", expand=True)
self.canvas.create_window((4, 4), window=self)
self.bind("<Configure>", self.OnFrameConfigure)
def OnFrameConfigure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def addrow(self, stuff, otherstuff):
global row
var = tk.StringVar(value=stuff)
entry = tk.Entry(self, textvariable=var)
entry.grid(row=row, column=0)
var = tk.StringVar(value=otherstuff)
entry = tk.Entry(self, textvariable=var)
entry.grid(row=row, column=1)
var = tk.StringVar(value="foobar")
entry = tk.Entry(self, textvariable=var)
entry.grid(row=row, column=3)
row += 1
def SomeProg():
for i in range(20):
stuff = "Stuff is " + str(i)
otherstuff = i * 4
win.addrow(stuff, otherstuff)
root = tk.Tk()
root.title("Stuff")
win = ProgramWindow()
SomeProg()
root.mainloop()