在"exec()"中全局名称'display'没有定义
我尝试使用exec()来执行一个简单的画线程序。这个程序运行得很好。但是当我通过tcp/ip网络发送同样的程序时(服务器读取程序并发送给客户端,客户端将其存储到一个字符串类型的变量(b)中),然后我在客户端使用exec(b)来执行它时,却出现了这样的错误:NameError: global name 'display' is not defined。
这个画线的代码是:
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys
name = 'line'
def display():
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glTranslatef(-1,-1,0)
gluLookAt(
0.1, 0.1, 0.3,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
glLineWidth(3.0)
color = [1.,1.,1.,1.]
glBegin(GL_LINES)
glVertex3f(0,0,0) # origin of the line
glVertex3f(.5,1.0,.9) # ending point of the line
glEnd()
glPopMatrix()
glutSwapBuffers()
return
def main():
glutInit(sys.argv)
print 'hello'
glutCreateWindow(name)
glClearColor(0.4,0.5,0.3,1.0)
glutDisplayFunc(display)
glutMainLoop()
return
main()
客户端代码的这一部分接收程序并将其存储到变量中,然后我们使用exec()来执行:
while f:
a = client.recv(1024)
if a=="#p":
f=0
break
b+=a
print b
exec(b)
代码执行到打印“hello”的部分时就停止了。
错误信息是:
hello
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "r13client.py", line 31, in run
exec(b)
File "<string>", line 34, in <module>
File "<string>", line 31, in main
NameError: global name 'display' is not defined
我不太明白这里出了什么问题。如果有人能帮我,我会非常感激。
2 个回答
0
如果你想看看一个关于网络和图形处理的例子,可以去看看这个链接:https://launchpad.net/game
0
你发送的字符串到底是什么,然后再执行它?
你的错误听起来像是你只发送了main()这个函数,这样是行不通的。