Home > other >  tkinter: Making a 3x3 grid with an image
tkinter: Making a 3x3 grid with an image

Time:11-30

I have these lines of code:

def initialize_board(self):
    for i in range(2):
        self.canvas.create_line((i   1) * size / 3, 0, (i   1) * size / 3, size)

    for i in range(2):
        self.canvas.create_line(0, (i   1) * size / 3, size, (i   1) * size / 3)

This makes two vertical lines and two horizontal lines. But I want to replace it with a 3x3 grid of an image of a square to represent the same thing. I'm making a tic tac toe game for reference.

This is the image I want to use:

self.empty_image = PhotoImage(file = 'book/pybook/image/empty.gif')

Can anyone help me?

CodePudding user response:

If you want to make a tic tac toe game you would be better off creating a 3x3 grid of buttons, then displaying the image of the X/O on the button.

Either way, your best bet is to create the widget and then call it onto the canvas with the grid method.

See Screenshot showing display of empty board

  • Related