May-09-2021, 01:48 PM
for example code run include imports and a snip it so others can easily copy and paste and run it.
Since canvas objects require exact coordinates the matrix style of two loops my not be the easiest way to accomplish what you want. If you are using buttons or labels it would.(by using the grid placement). I also suggest buttons, here's an example replace the text with a stock image maybe grass or sand, then when the button is pressed you give the user options to modify it.
Since canvas objects require exact coordinates the matrix style of two loops my not be the easiest way to accomplish what you want. If you are using buttons or labels it would.(by using the grid placement). I also suggest buttons, here's an example replace the text with a stock image maybe grass or sand, then when the button is pressed you give the user options to modify it.
from tkinter import Frame,Canvas,Tk,Button from functools import partial class Canvas_Grid(Frame): def __init__(self, parent= None,row=8,column=6): self.parent= parent self.parent.title('Canvas Grid 101') Frame.__init__(self, self.parent) self.pack(expand='yes',fill='both') self.canvas= Canvas(self) self.canvas.config(width=1000,height=600,bg='red') self.canvas.pack(expand='yes',fill='both') self.ROWS=row self.COLUMNS=column self.btns_list=[] for r in range(0,self.ROWS): for c in range(0,self.COLUMNS): text_= 'r:{},c:{}'.format(r,c) self.btns_list.append(Button(self.canvas,text=text_, command=partial(self.update_pic,r,c)) ) self.btns_list[-1].grid(row=r,column=c,padx=1,pady=1) def update_pic(self,row,col): print('row:{} column:{}'.format(row,col)) if __name__ == '__main__': root= Tk() cg=Canvas_Grid(root,row=10,column=10) root.mainloop()