0

I picked up this code and wanted to mess with it, the main problem i'm having is being unable to add an image to the actual gui at a set location on the 2 dimensional array. I get no actual error but also get no output of the image on the gui. Please help! Thank you.

import tkinter as tk
class GameBoard(tk.Frame):
    def __init__(self, parent, rows=9, columns=9, size=60, color1="light grey", color2="light grey"):
        '''size is the size of a square, in pixels'''

        self.rows = rows
        self.columns = columns
        self.size = size
        self.color1 = color1
        self.color2 = color2
        self.pieces = {}

        canvas_width = columns * size
        canvas_height = rows * size

        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0,
                            width=canvas_width, height=canvas_height, background="white")
        self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2)

        self.canvas.bind("<Configure>", self.refresh)

    def addpiece(self, name, image, row=1, column=1):
        bishop = tk.PhotoImage(file='C:\\Users\\Sharjeel Jan\\Desktop\\final shit man\\Pieces\\Bishop.gif')
        self.canvas.create_image(1,1, image=bishop, tags=(name, "Bishop"), anchor= "c")
        self.placepiece(name, row, column)

    def placepiece(self, name, row, column):
        '''Place a piece at the given row/column'''
        self.pieces[name] = (row, column)
        x0 = (column * self.size) + int(self.size/2)
        y0 = (row * self.size) + int(self.size/2)
        self.canvas.coords(name, x0, y0)

def placepiece(self, name, row, column): '''Place a piece at the given row/column''' self.pieces[name] = (row, column) x0 = (column * self.size) + int(self.size/2) y0 = (row * self.size) + int(self.size/2) self.canvas.coords(name, x0, y0)

  def refresh(self, event):
        '''Redraw the board, possibly in response to window being resized'''
        xsize = int((event.width-1) / self.columns)
        ysize = int((event.height-1) / self.rows)
        self.size = min(xsize, ysize)
        self.canvas.delete("square")
        color = self.color2
        for row in range(self.rows):
            color = self.color1 if color == self.color2 else self.color2
            for col in range(self.columns):
                x1 = (col * self.size)
                y1 = (row * self.size)
                x2 = x1 + self.size
                y2 = y1 + self.size
                self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=color, tags="square")
                color = self.color1 if color == self.color2 else self.color2
        for name in self.pieces:
            self.placepiece(name, self.pieces[name][0], self.pieces[name][1])
        self.canvas.tag_raise("piece")
        self.canvas.tag_lower("square")




if __name__ == "__main__":
    root = tk.Tk()
    board = GameBoard(root)
    board.pack(side="top", fill="both", expand="true", padx=4, pady=4)
  # player1 = tk.PhotoImage(data=imagedata)
  # board.addpiece("player1", player1, 0,0)
    root.mainloop()
sharjeel
  • 61
  • 9
  • 1
    Possible duplicate of [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – BingsF Jan 31 '18 at 22:05
  • i tried self.bishop to counter the garbage collection but got the same result. – sharjeel Jan 31 '18 at 22:53
  • Please format your code better using `{}` button above. – Nae Jan 31 '18 at 22:58
  • Please provide [mcve] for the specific question you're asking, as opposed to your current code. – Nae Jan 31 '18 at 22:59

1 Answers1

0

You have a single line that tries to put an image to GUI:

self.canvas.create_image(1,1, image=bishop, tags=(name, "Bishop"), anchor= "c")

which is under addpiece.

The line below saves the image in a reference:

bishop = tk.PhotoImage(file='C:\\Users\\Sharjeel Jan\\Desktop\\final shit man\\Pieces\\Bishop.gif')

which is defined exclusively for the scope of addpiece, when the method is finished, the image disappears if at all being displayed.

Which effectively makes this question a duplicate to Why does Tkinter image not show up if created in a function?


In order to prevent that, make the image reference available in the scope where mainloop is called.

Either attach the reference bishop to an object, for example self.canvas, whose reference is available in the scope of mainloop:

self.canvas.bishop = tk.PhotoImage(file='C:\\Users\\Sharjeel Jan\\Desktop\\final shit man\\Pieces\\Bishop.gif')
self.canvas.create_image(1,1, image=self.canvas.bishop, tags=(name, "Bishop"), anchor= "c")

or simply pass the image reference from the mainloop scope is in, and use that object reference:

self.canvas.create_image(1,1, image=image, tags=(name, "Bishop"), anchor= "c")
Nae
  • 14,209
  • 7
  • 52
  • 79