如何使局部变量用于全局变量?

2024-03-29 12:17:21 发布

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

所以我尝试用python编写csv中的行,我的代码如下所示。但它不会写,因为这些变量是局部的?我该怎么做?谢谢您!没有错误显示,它在csv中不写行。 当我在mac上的pycharm中运行它时,它可以工作,但在我将它设置为windows上的exe后,它不会写入行。你知道吗

types=[]
row=[]
col=[]
image_number=[]
def click(event):
    global rectangle
    global image_list1
    types.append(v.get())
    row.append(event.x)
    col.append(event.y)
    filename = image_list1[0]
    image_number.append(filename)
    x1, y1 = (event.x -3), (event.y- 3)
    x2, y2 = (event.x + 3), (event.y + 3)
    rectangle=w.create_rectangle(x1, y1, x2, y2,fill=color[v.get()-1],outline="")
if image_length:
    root.tk()
    w.bind("<Button-1>", click)
    root=mainloop()
    d.writerows(zip(image_number, types, row, col))

Tags: csvimageeventnumbergetcolfilenameglobal
1条回答
网友
1楼 · 发布于 2024-03-29 12:17:21

这就是return语句的作用:

types=[]
row=[]
col=[]
image_number=[]
def click(event):
    global rectangle
    global image_enum
    global image_list1
    types.append(v.get())
    row.append(event.x)
    col.append(event.y)
    filename = image_list1[0]
    image_number.append(filename)
    return (image_number, types, row, col)
if image_length:
    image_number, types, row, col = click(event)
    d.writerows(zip(image_number, types, row, col))

或者如果你想变得花哨:

if image_length:
    d.writerows(zip(*click(event)))

当然,您需要先定义一个事件!你知道吗

相关问题 更多 >