我是python的新手,从我十几岁起就对C ++有一定的经验,所以请保持冷静。
我想做的事情-左键单击添加一个点(到二维数组)并增加pointsCount变量。我正在像这样使用Tkinter绑定:
canvas.bind("<Button-1>",lambda event: leftClick(event,pointsCounter,points))
左键单击功能的定义如下:
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)
虽然点的添加工作正常,但numberOfPoints仅在第一次单击后才增加。 我了解Python仅将一个值传递给该函数,因此我无法对其进行更改。但是它适用于数组。右键单击时,有什么方法可以增加numberOfPoints。
这是完整的代码
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.y)
def rightClick(event):
print("Right ",event.y)
def leftClick(event,pointsTable):
print("Left ",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>",points))
canvas.bind("<Button-2>",middleclick)
canvas.bind("<Button-3>",rightClick)
canvas.pack()
root.mainloop()```
I'd be really grateful for some pointers.