2

出现包含 GLUT 图形的窗口后,我想在终端中输入:

user@computer: python woop.py
# Now displaying a beautiful landscape
(cmd): season winter
# Now changing season to winter
(cmd): event meteor
# Now meteoring otherwise peaceful landscape
(cmd): season summer
# Now changing season to summer
(cmd): exit
#bye ^_^
user@computer:

理想情况下,我想将 python cmd与 GLUT 的glutKeyboardFunc 集成。我的尝试失败了(一次允许一个或另一个,而不是两者。还有窗口或终端是否有焦点的问题)。

这是一些示例代码,它显示了一个旋转的茶壶。目前,按下“m”将调用流星善良(存根),但能够输入例如“meteor 500”将是更可取的。

#! /usr/bin/env python
''' 
Code is a reduced version of http://www.seethroughskin.com/blog/?p=771
'''
import OpenGL 
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

import time, sys

class dizzyTea:

    global rotY

    def __init__(self):
        self.main()

    def InitGL(self,Width, Height):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glClearDepth(1.0)
        glShadeModel(GL_SMOOTH)  
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()       
        gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)     

    # The main drawing function. 
    def DrawGLScene(self):
        global rotY
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()                    # Reset The View 
        glTranslatef(-0.5, 0.0, -6.0)
        glRotatef(rotY,0.0,1.0,0.0)
        glutWireTeapot(1.0)
        glScalef(0.3,0.3,0.3)
        glutSwapBuffers()
        rotY += 1.0

    # The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
    def keyPressed(self,*args):

        # If escape is pressed, kill everything.
        if args[0] == '\x1b':
            sys.exit()
        elif args[0] == 'm':
            print "Now meteoring otherwise peaceful teapot"
            # meteor shenanigans

    def main(self):
        global window
        global rotY
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
        glutInitWindowSize(640, 480)
        glutInitWindowPosition(0, 0)
        window = glutCreateWindow("Jeff Molofee's desecrated GL Code Tutorial")
        glutDisplayFunc(self.DrawGLScene)
        glutIdleFunc(self.DrawGLScene)
        glutKeyboardFunc(self.keyPressed)
        self.InitGL(800, 600)
        rotY = 0.0
        glutMainLoop()

if __name__ == "__main__":
    x = dizzyTea()

我可以使用glutKeyboardFunc将字符收集到一个全局字符串中,提供相同的功能效果,但是用户会盲目输入。“print somestring”允许在同一行打印,但是逗号表示在键入时不会显示输出。“print '\b'”(退格)也不能普遍工作......

基本上我不想拥有:

user@computer: python woop.py
# Now displaying a beautiful landscape
(cmd): s
(cmd): se
(cmd): sea
(cmd): seas
...etc

输入一个命令

使用限制:

  • pyopengl
  • 过剩

(尽管对于希望解决不同问题的任性未来的人们欢迎其他答案)

4

2 回答 2

2
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
def keyPressed(self,*args):
    if args[0] == '\x08':
        self.keyCache = self.keyCache[:-1]
    elif args[0] == '\x1b':
        sys.exit()
    elif args[0] == 'm':
        print "Now meteoring otherwise peaceful teapot"
        # meteor shenanigans
    else:
        self.keyCache += args[0]
    sys.stdout.write(self.keyCache +"                                                  \r")#print "keypress: <",self.keyCache,">"
    sys.stdout.flush()

并添加一个新的类变量'keyCache'。

然后只需使用正常的打印刷新将数据写入同一行。唯一的一点是,您必须在缓存的击键后写几个空格,否则当您使用退格键时,已删除的元素仍会出现在屏幕上。

另一种选择是并行键盘线程,仅用于处理来自命令行的击键。最大的问题是,当窗口关闭时,过剩并没有提供一个很好的回调,所以你必须找出另一种方法来杀死你的线程。

于 2011-11-30T03:17:15.187 回答
1

您应该使用 cmd [0] 框架来构建您的解释器,并在单独的线程中启动 cmd.cmdloop。0. http://docs.python.org/3/library/cmd.html

于 2013-07-03T08:09:56.843 回答