I'm very new to python having some experience in C++ from my teenage years so please be gentle.
What I'm trying to do - left click adds a point (to a 2 dimensional array) and increments the pointsCount variable. I'm using Tkinter binding like so:
canvas.bind("<Button-1>", lambda event: leftClick(event,pointsCounter,points))
the left click function is defined like so:
def leftClick(event, numberOfPoints,pointsTable):
print("Left ", event.x ," ", event.y)
x = roundup(event.x, 50)
y = roundup(event.y, 50)
pointsTable.append([x,y])
numberOfPoints = numberOfPoints + 1
print(pointsTable)
print(numberOfPoints)
While the appending of points works fine the numberOfPoints is only incremented after the first click. I understand that Python only passes a value to the function so I can't change it. However it works for the array. Is there any way I can increment the numberOfPoints when I right click.
Here's the entire code
import array
import math
from tkinter import Canvas
def roundup(x, n=10):
res = math.ceil(x/n)*n
if (x%n < n/2)and (x%n>0):
res-=n
return res
def middleClick(event):
print("Middle ", event.x ," ", event.y)
def rightClick(event):
print("Right ", event.x ," ", event.y)
def leftClick(event, numberOfPoints,pointsTable):
print("Left ", event.x ," ", event.y)
x = roundup(event.x, 50)
y = roundup(event.y, 50)
pointsTable.append([x,y])
numberOfPoints = numberOfPoints + 1
print(pointsTable)
print(numberOfPoints)
for i in range(1, 5):
canvas.create_oval(pointsTable[i][0] - radius, pointsTable[i][1] - radius, pointsTable[i][0] + radius, pointsTable[i][1] + radius, fill="red")
return numberOfPoints
root = Tk()
canvas = Canvas(root, width = 800, height = 800)
radius = 5
points = [[]]
pointsCounter = 1
canvas.bind("<Button-1>", lambda event: leftClick(event,pointsCounter,points))
canvas.bind("<Button-2>",middleClick)
canvas.bind("<Button-3>",rightClick)
canvas.pack()
root.mainloop()```
I'd be really grateful for some pointers.