如何在使用事件指针时更改全局变量

2024-06-02 18:16:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python有一些很新的经验,所以在十几岁时有一些C++经验。p>

我要做的是-左键单击添加一个点(到一个二维数组)并增加pointscont变量。我使用的是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)

虽然点的附加工作正常,但点的数量仅在第一次单击后增加。 我知道Python只向函数传递一个值,所以我不能更改它。但是它对数组有效。有什么方法可以增加我右击时的点数吗

这是全部代码

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. 



Tags: importeventbinddefbuttonrespointscanvas
1条回答
网友
1楼 · 发布于 2024-06-02 18:16:20

pointscont和pointsTable作为函数参数的区别在于,pointscont是不可变的,变量一旦改变就被分配给一个新的对象。pointsTable是可变的,对象本身在更改时会被修改。也就是说,当函数中的pointsTable被修改时,更改会反映在原始变量中。对于pointscont,将创建一个值为pointscont+1的新对象,并且pointCountinside函数现在指向它。函数外的pointscont仍然指向零对象

一种解决方案是将points设为全局变量,左键单击下面的。另一种是根本不存储pointscont,而是依赖于下面的pointsTable,leftClick1的长度

from random import randint

pointsTable = []
numberOfPoints = 0

class Event:  # Mock up to test code.
    def __init__(self):
        self.x = randint(0,50)
        self.y = randint(0,50)

# Keeping external counter
def leftClick(event, pointsTable):
    global numberOfPoints
    print("Left ", event.x ," ", event.y)
    x = event.x
    y = event.y
    pointsTable.append([x,y])
    numberOfPoints = numberOfPoints + 1
    print(pointsTable)
    print(numberOfPoints)

def number_of_points(): 
# This may be overkill len(pointsTable) could be used instead
    return len(pointsTable)

# Using len(pointsTable)
def leftClick1(event, pointsTable):
    print("Left ", event.x ," ", event.y)
    x = event.x
    y = event.y
    pointsTable.append([x,y])
    print(pointsTable)
    print(number_of_points())

for _ in range(2):
    leftClick( Event(), pointsTable )

for _ in range(2):
    leftClick1( Event(), pointsTable )

""" Output 
Left  5   4
[[5, 4]]
1
Left  49   6
[[5, 4], [49, 6]]
2
Left  44   20
[[5, 4], [49, 6], [44, 20]]
3
Left  6   30
[[5, 4], [49, 6], [44, 20], [6, 30]]
4
"""

我会选择依赖于pointsTable的长度,因为它更简单、更健壮

相关问题 更多 >