from tkinter import * class Application(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.cell = [] self.pack() self.buildscrbars() self.cnvs_cell_holder = Canvas(bg = "white", xscrollcommand = self.scr_barX, yscrollcommand = self.scr_barY) self.cnvs_cell_holder.pack(side = BOTTOM, fill = BOTH, expand = YES) self.width = 0 self.createCells([[1,2,1],[2,1,2]]) self.attachscrbars() def createCells(self, array = []): '''Preconditions: Array is a list of lists, all of which have at least 1 element, no list has a non-integer element''' '''Creates a grid of colored buttons based on array''' x = 0 while x < len(array): y = 0 while y < len(array[x]): bkgd = self.colorCell(array[x][y]) self.btn_base = Button(self.cnvs_cell_holder, text=array[x][y], bg= bkgd, activebackground = bkgd) self.cell.append(self.btn_base) self.btn_base.grid(column = x, row = y) y += 1 x += 1 self.width = x def colorCell(self, definer = 0): '''This will be defined by each creature as a get_color function''' if definer == 0: return "black" elif definer == 1: return "blue" elif definer == 2: return "green" else: return "red" def buildscrbars(self): self.scr_barY = Scrollbar(self, orient=VERTICAL) self.scr_barX = Scrollbar(self, orient=HORIZONTAL) def attachscrbars(self): self.scr_barY.pack(side=TOP, fill = Y, expand = YES) self.scr_barX.pack(side=TOP, fill = X, expand = YES) app = Application() app.master.title("Sample application") app.mainloop()