2017-07-20 183 views
-1

所以我想在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)) 

回答

1

这是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))) 

当然,你需要首先定义一个事件!

+0

您也可以只写'd.writerows(邮政编码(*点击(事件)))' – Zizouz212

+0

啊,正要编辑在:) – perigon

+0

你们两个数秒打我:P – Zizouz212