423,688 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 IT Pros & Developers. It's quick & easy.

Trouble displaying image on Tkinter Board that i have

P: 0
i have a chessboard that i want to add an image of a king to, i add the image at a set location but the image doesn't appear on the board, please help!.

class GameBoard(tk.Frame):
def __init__(self, parent, rows=8, columns=8, size=70, color1="white", color2="lightgrey"):
'''size is 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, image, row = 6 , columns = 6):
self.canvas.WhiteKing = tk.PhotoImage(file = 'E:\\Final Project + Report\\Pieces\\WhiteKing.png')
self.canvas.create_image(6,6, image = self.canvas.WhiteKing, anchor = NW)

def KingMoves(self, row, columns):
PossibleMoves = []

def refresh(self, event):
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
Feb 4 '18 #1
Share this Question
Share on Google+
2 Replies


P: 1
very interesting and cognitive gizmo
Feb 5 '18 #2

Expert 100+
P: 597
See the green background box at http://effbot.org/tkinterbook/photoimage.htm
Feb 5 '18 #3

Post your reply

Sign in to post your reply or Sign up for a free account.