I was able to recreate the issue in a much smaller, 50-line code. This is why you may see a seemingly 'useless' need for parameters and arguments.
First what the program does, is create a main window and gameboard object which is a subclass of tk.Canvas
. The object then creates 8 rows and columns with alternating colors for each 120x120 square.
Then, I create the game pieces (which, will normally be done in a for loop, but this run-down example, just creates two pieces) giving them a 60x60 and a 60x120 spot respectively.
Finally I wrap it up with a simple quit button and mainloop
function.
My code seems to cause a new canvas to get drawn on the GameBoard each time I try to place an image item on the board.
Now, I believe that this error stems from the GameBoard class, specifically where it calls the __init__
method from the base class.
I've tried working and reworking all different places to put each self
method thinking that that might help, but to no avail.
However, if I comment out the two lines where I create the rook and pawn, then the board appears like normal. So I know that it must be caused directly by the pieces.
So, here is my attempt at a chess game. Please keep in mind it's nowhere near complete, and the code below is not an excerpt from my code, but a total recreation just to show the error.
If you see any glaring issues that have nothing to due with my actual problem, know that I'm trying my hardest to keep from repeating myself, and following the pip8 guidelines. So any input on cleanliness, or overall functionality would be greatly appreciated.
As quamrana had pointed out in the comments: The reason why I have GamePiece inherit from GameBoard, is specifically because I want to not have to worry about changing the background of the image to match the color of the space that said image is on.
import tkinter as tk
from PIL import ImageTk, Image
class GameBoard(tk.Canvas):
def __init__(self, parent):
self.color = ""
#where I believe my error comes from
tk.Canvas.__init__(self, parent, height=960, width=960)
self.grid()
#spaces created here by rows snd columns
def create_spaces(self):
x1, y1, x2, y2 = 0, -120, 120, 0
for _row in range(8):
x1, x2 = 0, 120
y1 += 120
y2 += 120
self.alternate_colors()
for _column in range(8):
self.create_rectangle(x1, y1, x2, y2, fill=self.color)
x1 += 120
x2 += 120
self.alternate_colors()
#each space created alternates between the two colors here
def alternate_colors(self):
if self.color == "green": self.color = "pink"
else: self.color = "green"
class GamePiece(GameBoard):
def __init__(self, parent, xPOS, yPOS, photo_file):
GameBoard.__init__(self, parent)
self.photo1 = Image.open(photo_file)
self.photo2 = ImageTk.PhotoImage(self.photo1)
self.create_image(xPOS, yPOS, image=self.photo2)
self.grid()
#main window and gameboard is created here
window = tk.Tk()
gameboard = GameBoard(window)
gameboard.create_spaces()
#all pieces will eventually be created here using for loops
gamepiece1 = GamePiece(gameboard, 60, 60, "black_rook.png")
gamepiece2 = GamePiece(gameboard, 60, 120, "black_pawn.png")
#quit button and mainloop
tk.Button(window, text="Quit", command=quit, bd=5).grid(pady=40)
window.mainloop()