全局名称“显示”在“exec()”期间未定义

发布于 2024-10-11 15:58:01 字数 1474 浏览 1 评论 0原文

我尝试使用 exec() 执行一个简单的画线程序。 效果很好。但是当我尝试通过 tcp/ip 网络发送它来执行相同的程序时(服务器读取程序并将其发送到客户端,客户端将其接收到字符串类型的变量(b)),然后我使用 exec(b )在客户端执行它,但它说: NameError: 全局名称 'display' 未定义

画线代码是:

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) 

代码执行到给出 print 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

我无法理解这里出了什么问题。如果有人可以帮助我,我将不胜感激。

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()
 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()

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半﹌身腐败 2024-10-18 15:58:01

您发送然后执行的字符串到底是什么?

您的错误听起来像是您只发送了 main() 函数,这不起作用。

What exactly is the string you send and then exec?

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

十秒萌定你 2024-10-18 15:58:01

有关网络和图形的一种方法的示例,请查看 https://launchpad.net/game

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文