Python 全局变量未更新(Processing.py)
我正在使用processing.py制作一个用于绘制简单线条的应用程序 这是我的代码:Python 全局变量未更新(Processing.py),python,variables,global-variables,processing,Python,Variables,Global Variables,Processing,我正在使用processing.py制作一个用于绘制简单线条的应用程序 这是我的代码: pointsList =[ ] points = [] def setup(): global pointsList, points size(400,400) stroke(255) strokeWeight(5) def draw(): global pointsList, points background(0) for points in
pointsList =[ ]
points = []
def setup():
global pointsList, points
size(400,400)
stroke(255)
strokeWeight(5)
def draw():
global pointsList, points
background(0)
for points in pointsList:
draw_points(points)
draw_points(points)
def keyPressed():
global pointsList, points
if key == 'e':
try:
pointsList.append(points)
points = [] #<--- this right here not updating
except Exception as e:
print(e)
print(pointsList)
def mouseClicked():
global points
print(points)
points.append((mouseX,mouseY))
def draw_points(points):
for i in range(len(points)-1):
draw_line(points[i],points[i+1])
def draw_line(p1,p2):
line(p1[0],p1[1],p2[0],p2[1])
pointsList=[]
点数=[]
def设置():
全局点列表,点
尺寸(400400)
笔划(255)
冲程重量(5)
def draw():
全局点列表,点
背景(0)
对于pointsList中的点:
绘制点(点)
绘制点(点)
按def键():
全局点列表,点
如果键==“e”:
尝试:
pointsList.append(点)
points=[]#问题位于不同的位置,因为在函数draw()
中对点使用相同的名称points
,因此它将pointList
中的最后一个元素分配给points
您必须在draw()
-ie.项目中使用不同的名称
def draw():
global pointsList, points
background(0)
for items in pointsList: # <-- use `items`
draw_points(items) # <-- use `items`
draw_points(points)
def draw():
全局点列表,点
背景(0)
对于点列表中的项目:#问题位于不同的位置,因为在函数draw()
in-loop中为点使用相同的名称points
,因此它将点列表中的最后一个元素指定给点
您必须在draw()
-ie.项目中使用不同的名称
def draw():
global pointsList, points
background(0)
for items in pointsList: # <-- use `items`
draw_points(items) # <-- use `items`
draw_points(points)
def draw():
全局点列表,点
背景(0)
对于pointsList中的项目:#您如何知道它不属于udpating?您是否使用print(points)
来更改它?或者可能它从未运行过这部分代码,因此无法更新。@furas yes I'v检查了打印功能,它在“按键”功能中进行了更新,但在“绘图”或“鼠标点击”功能中是相同的。您是否检查了它是否在中运行代码,如果key='e':
?也许键
不是e
而是e
?@furas是的,它是“e”,我运行了它,它确实清除了列表,但在几次更改后,它清除了列表,但代码似乎相同。现在我试着找出为什么它现在能正常工作。你怎么知道它不起作用?您是否使用print(points)
来更改它?或者可能它从未运行过这部分代码,因此无法更新。@furas yes I'v检查了打印功能,它在“按键”功能中进行了更新,但在“绘图”或“鼠标点击”功能中是相同的。您是否检查了它是否在中运行代码,如果key='e':
?也许键
不是e
而是e
?@furas是的,它是“e”,我运行了它,它确实清除了列表,但在几次更改后,它清除了列表,但代码似乎相同。现在我试图找出为什么它现在能正常工作。