Glut方法即使使用dll也无法识别(Pyopengl3/Python3.8/Windows1064)

2024-04-16 13:04:07 发布

您现在位置:Python中文网/ 问答频道 /正文

在运行通过Pip安装的Pyopengl3.1.5(Python3.8-Windows10Pro/64位)中的主脚本时,编译器不会识别Glut方法

遵循这些stackoverflow答案(1&;2)后,即重新安装Pyopengl wheel&;将dll's放入主脚本文件夹(C:…Python\…\site packages-PyOpengl的主目录)、环境路径、System32&;SysWow64,编译器仍会给出相同的错误:

import OpenGL.GLUT  
glutInit()
NameError: name 'glutInit' is not defined (# checked for casetype )

但是,在Site packages\Opengl\Glut中有一个名为“special.py”的python脚本,该脚本定义了Glut方法。因此,在将glutinit方法的路径添加到init.py(Glut目录)并进行编译时,编译器仍然会给出以下错误

OpenGL\GLUT\special.py:- def glutInit(INITIALIZED = False)   

OpenGL\GLUT\init.py:- from OpenGL.GLUT.special import *
                      from OpenGL.GLUT.special import glutInit (#added)  


OpenGL\GLUT\main.py:- import OpenGL.GLUT
                      import OpenGL.GLUT.special(#added) 
                      import OpenGL.GLUT.special.glutInit (#added)   

                      glutInit(INITIALIZED = True)  (# function call)

                      ModuleNotFoundError: No module named 'OpenGL.GLUT.special.glutInit'; 'OpenGL.GLUT.special' is not a package  

因此问题是-如何让编译器识别special.py中的glut方法,以及是否有任何方法更新.pyc文件以反映更新的init.py导入路径

Pyopengl主脚本(stack滥用职权网站)

import OpenGL
import OpenGL.GL
import OpenGL.GLUT
import OpenGL.GLUT.special #(added)
import OpenGL.GLUT.special.glutInit #(added)
import OpenGL.GLU
print("Imports successful!")

w, h = 500,500

# define square 
def square():
    # We have to declare the points in this sequence: bottom left, bottom right, top right, top left
glBegin(GL_QUADS) # Begin the sketch
glVertex2f(100, 100) # Coordinates for the bottom left point
glVertex2f(200, 100) # Coordinates for the bottom right point
glVertex2f(200, 200) # Coordinates for the top right point
glVertex2f(100, 200) # Coordinates for the top left point
glEnd() # Mark the end of drawing


# draw square
def showScreen():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Remove everything from screen (i.e. displays all white)
    glLoadIdentity() # Reset all graphic/shape's position
    square() # Draw a square using our function
    glutSwapBuffers()

# Initialise and create Opengl screen
glutInit(True)
glutInitDisplayMode(GLUT_RGBA) # Set the display mode to be colored
glutInitWindowSize(500, 500)   # Set the w and h of your window
glutInitWindowPosition(0, 0)   # Set the position at which this windows should appear
wind = glutCreateWindow("OpenGL Coding Practice") # Set a window title
glutDisplayFunc(showScreen)
glutIdleFunc(showScreen) # Keeps the window open
glutMainLoop()  # Keeps the above created window displaying/running in a loop

Tags: the方法pyimport脚本addedfor编译器
1条回答
网友
1楼 · 发布于 2024-04-16 13:04:07

在每次函数调用时,您必须在模块路径之前:

例如:

glutInit(True)

OpenGL.GLUT.glutInit()

或者您必须使用from子句来更改^{} statements

例如:

from OpenGL import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

相关问题 更多 >