Python Forum
[Tkinter] Draw a grid of Tkinter Canvas Rectangles
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Draw a grid of Tkinter Canvas Rectangles
#6
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.
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()
Reply


Messages In This Thread
Draw a grid of Tkinter Canvas Rectangles - by MrTim - May-08-2021, 12:46 PM
RE: Draw a grid of Tkinter Canvas Rectangles - by joe_momma - May-09-2021, 01:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Can i draw more elements in a canvas? Yeti 2 1,935 Dec-14-2023, 04:46 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 15,459 May-25-2023, 07:37 PM
Last Post: deanhystad
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 11,057 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 13,602 May-11-2021, 10:01 PM
Last Post: MrTim
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 6,486 Mar-27-2021, 05:04 AM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 21,445 Feb-06-2021, 03:35 PM
Last Post: joe_momma
  how to rotate lines in tkinter canvas helpmewithpython 1 4,487 Oct-06-2020, 06:56 PM
Last Post: deanhystad
  Tkinter function to clear old canvas and start a fresh "operation" zazas321 5 14,811 Oct-01-2020, 04:16 AM
Last Post: zazas321
  question on tkinter canvas PhotoImage gr3yali3n 1 2,865 Sep-05-2020, 12:18 PM
Last Post: Larz60+
  [Tkinter] how to draw dynamic moving scale and potting trace point on waveform in tkinter pytho sameer_1985 0 2,628 May-31-2020, 01:52 PM
Last Post: sameer_1985

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020