from tkinter import * #controlling class for the grid class Grid: def __init__(self,myParent,x,y): self.myParent = myParent self.cell = [[None for j in range(x)] for i in range(y)] self.color_prevButton="red" #make a canvas to hold the cells self.cnvs_cell_holder = Canvas(self.myParent, bg = "white",width=600,\ height=400,scrollregion=(0,0,1200,800)) #packs the canvas into the parent frame self.cnvs_cell_holder.pack(side = BOTTOM, fill = BOTH,\ expand = YES) #self.cnvs_cell_holder.grid(row=x, column=y, sticky=N+S+E+W) self.createCells(x,y) #creates a grid of buttons with the give dimensions def createCells(self, x, y): i = 0 while(i < x): j = 0 while(j < y): self.btn_base = Button(self.cnvs_cell_holder,\ bg= "white",\ activebackground = "black") self.cell[i][j]=(self.btn_base) self.btn_base.grid(column=i,row=j) def handler(event, i=i, j=j): return self.btn_GridClick(event, i, j) self.btn_base.bind(sequence="",\ func=handler) j += 1 i += 1 #handler for buttons in the grid def btn_GridClick(self, event, i, j): event.widget.configure(bg=self.color_prevButton) print("i="+str(i)+",j="+str(j)) #self.localWorld.setCreature(i,j,self.color_prevButton) #builds horizontal and vertical scrollbars def buildScrollBars(self, x,y): self.frame.grid_rowconfigure(0, weight=1) self.frame.grid_columnconfigure(0, weight=1) self.scr_barY.grid(row=0, column=1, sticky=N+S,) self.scr_barX.grid(row=1, column=0, sticky=E+W ) self.cnvs_cell_holder.grid(row=x, column=y, sticky=N+S+E+W) self.scr_barX.config(command=self.cnvs_cell_holder.xview) self.scr_barY.config(command=self.cnvs_cell_holder.yview) self.frame.pack() root=Tk() app = Grid(root,10,10) root.mainloop()