Reputation: 589
I'm new to python and tkinter and I have been looking for a solution to this issue for a while.
I am creating a sudoku board and filling in the known values using tkinter. I want to go back and delete some of the numbers at known positions by I can't figure out how to do this using the delete function.
My board is build as follows:
for i in range(10)
color = "blue" if i%3 ==0 else "gray"
thickness = 5 if i% 3 ==0 else 1
x0 = 10 + i* 5
y0 = 10
x1 = 10+ i * 5
y1 = 500 - 10
canvas.create_line(x0,y0,x1,y1, fill=color, width=thickness)
x0 = 10
y0 = 10 + i * 5
x1 = 500 - 10
y1 10 + i *5
canvas.create_line(x0,y0,x1,y1, fill=color, width=thickness)
My function to add the numbers to the board is as follows:
def uploadBoard():
for i in range(9):
for j in range(9):
number = puzzleBoard[i][j]
if number != 0:
x = 10 + j * 5 + 5/2
y = 10 + i * 5 + 5/2
canvas.create_text(x,y, text=number)
The puzzleBoard
is an array of arrays containing numbers.
What I want to do is create a function that will delete a number at a specific (i,j) location when the corresponding value in puzzleBoard[i][j]
has been changed to 0.
Upvotes: 1
Views: 193
Reputation: 385830
When you create an item on the canvas, the canvas returns an identifier. You can use this identifier later to modify or delete the item. You simply need to save this identifier.
Example:
First, save a reference when you create the items. Start by creating an empty dictionary, and then using the tuple (i,j)
as the key:
item = {}
for i in range(9):
for j in range(9):
...
item[(i,j)] = canvas.create_text(x,y, text=number)
Next, use the item id you've saved to delete the item:
canvas.delete(item[(i,j)])
Upvotes: 2