开发者

Global name 'display' is not defined during "exec()"

开发者 https://www.devze.com 2023-02-03 15:37 出处:网络
I tried executing a simple line drawing program using exec(). It worked fine. But when I tried to execute the same program by sending it through a tcp/ip network(the server reads the program and sends

I tried executing a simple line drawing program using exec(). It worked fine. But when I tried to execute the same program by sending it through a tcp/ip network(the server reads the program and sends it to the client which receives it to a variable(b) of string type) and then i use exec(b) in the client to execute it but it says: NameError: global name 'display' is not defined

The line drawing code is:

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()
 glu开发者_JS百科tSwapBuffers()
 return
def main():
 glutInit(sys.argv)
 print 'hello'
 glutCreateWindow(name)
 glClearColor(0.4,0.5,0.3,1.0)
 glutDisplayFunc(display)
 glutMainLoop()
 return
main()

This part of the client code receives the program and stores it into the variable and then we use exec():

while f: 
   a = client.recv(1024)
   if a=="#p":
    f=0
    break
   b+=a

  print b

  exec(b) 

The code executes upto the part where print hello is given and then stops.

The error message:

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

I am unable to understand what is going wrong here. If anyone could help I'd be grateful.


What exactly is the string you send and then exec?

Your error sounds like you only send the main() function, which won't work.


For an example of one approach to networking and graphics, take a look at https://launchpad.net/game

0

精彩评论

暂无评论...
验证码 换一张
取 消