Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SyntaxError:name";“是”;在全局变量之前使用_Python_Variables_Tkinter_Syntax Error_Global Variables - Fatal编程技术网

Python SyntaxError:name";“是”;在全局变量之前使用

Python SyntaxError:name";“是”;在全局变量之前使用,python,variables,tkinter,syntax-error,global-variables,Python,Variables,Tkinter,Syntax Error,Global Variables,我在这里写这段代码: import tkinter canvas = tkinter.Canvas() canvas.pack() def ball(): canvas.delete('all') canvas.create_oval(x-5, y-5, x+5, y+5) global y y = y+5 if y<200: canvas.after(100, ball) 导入tkinter canvas=tkinter.can

我在这里写这段代码:

import tkinter
canvas = tkinter.Canvas()
canvas.pack()

def ball():
    canvas.delete('all')
    canvas.create_oval(x-5, y-5, x+5, y+5)
    global y
    y = y+5
    if y<200:
        canvas.after(100, ball)
导入tkinter
canvas=tkinter.canvas()
canvas.pack()
def ball():
canvas.delete('all'))
画布。创建椭圆(x-5,y-5,x+5,y+5)
全局y
y=y+5

如果y调用
canvas.create_oval(x-5,y-5,x+5,y+5)
references
y
,然后调用
global y
。翻转顺序,您应该可以:

def ball():
全球y#甚至可以在稍后完成,但提前完成会更整洁
canvas.delete('all'))
画布。创建椭圆(x-5,y-5,x+5,y+5)
y=y+5

如果y
canvas.create_oval(x-5,**y-5**,x+5,**y+5**)
这个错误说明了一切。在第7行使用
y
,这是在第8行声明
全局y
变量之前。您应该在使用变量之前声明它