如何解决该错误。AttributeError: module 'pyglfw' has no attribute 'pyglfwInit'.

问题描述 投票:1回答:1

我想用python做一个openGL项目。下面的代码正在创建一个窗口来显示图形。

import os

import glfw
from OpenGL.GL import *


class renderwindow():
    '''GLFW Renderting window class'''

    def __init__(self):

        #save current working directory
        cwd = os.getcwd()

        #initialize glfw
        glfw.glfwInit()

        #restore cws
        os.chdir(cwd)

        #version hints
        glfw.glfwWindowHint()

        glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MAJOR, 3)
        glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MINOR, 3)
        glfw.glfwWindowHint(glfw.GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
        glfw.glfwWindowHint(glfw.GLFW_OPENGL_PROFILE,
                            glfw.GLFW_OPENGL_CORE_PROFILE)

        # make a window
        self.width, self.height = 640, 480
        self.aspect = self.width / float(self.height)
        self.win = glfw.glfwCreateWindow(self.width, self.height,
                                         b'simpleglfw')

        # make the context current
        glfw.glfwMakeContextCurrent(self.win)

    def main(self):
        glViewport(0, 0, self.width, self.height)
        glEnable(GL_DEPTH_TEST)
        glClearColor(0.5, 0.5, 0.5, 1.0)

当我运行这段代码时,它报告了这样的错误。

Traceback (most recent call last):
  File "/snap/pycharm-community/192/plugins/python-ce/helpers/pydev/pydevd.py", line 1438, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-community/192/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/surface/Final-Year-Project/FYP/Main.py", line 6, in <module>
    class main():
  File "/home/surface/Final-Year-Project/FYP/Main.py", line 29, in main
    rw = renderwindow()
  File "/home/surface/Final-Year-Project/FYP/Open_GL_project1/RenderWIndow.py", line 16, in __init__
    glfw.glfwInit()
AttributeError: module 'glfw' has no attribute 'glfwInit'

Process finished with exit code 1

我在网上搜索了一下解决办法,有人说是旧版GLFW引起的问题。pycharm与GLFW不匹配。我不知道该如何解决

python ubuntu opengl glfw
1个回答
1
投票

该方法的名称不是 glfwInit它是 init. 这也适用于其他方法。(window_hint, create_window, make_context_current):

import os

import glfw
from OpenGL.GL import *

class renderwindow():
    '''GLFW Renderting window class'''

    def __init__(self):

        #save current working directory
        cwd = os.getcwd()

        #initialize glfw
        glfw.init()

        #restore cws
        os.chdir(cwd)

        #version hints
        glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
        glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
        glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
        glfw.window_hint(glfw.OPENGL_PROFILE,
                            glfw.OPENGL_CORE_PROFILE)

        # make a window
        self.width, self.height = 640, 480
        self.aspect = self.width / float(self.height)
        self.win = glfw.create_window(self.width, self.height,
                                         'simpleglfw', None, None)

        # make the context current
        glfw.make_context_current(self.win)

    def main(self):
        glViewport(0, 0, self.width, self.height)
        glEnable(GL_DEPTH_TEST)
        glClearColor(0.5, 0.5, 0.5, 1.0)

或者 import glfw.GLFW as glfw:

import os

#import glfw
import glfw.GLFW as glfw

from OpenGL.GL import *

class renderwindow():
    '''GLFW Renderting window class'''

    def __init__(self):

        #save current working directory
        cwd = os.getcwd()

        #initialize glfw
        glfw.glfwInit()

        #restore cws
        os.chdir(cwd)

        #version hints
        glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MAJOR, 3)
        glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MINOR, 3)
        glfw.glfwWindowHint(glfw.GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
        glfw.glfwWindowHint(glfw.GLFW_OPENGL_PROFILE,
                            glfw.GLFW_OPENGL_CORE_PROFILE)

        # make a window
        self.width, self.height = 640, 480
        self.aspect = self.width / float(self.height)
        self.win = glfw.glfwCreateWindow(self.width, self.height,
                                         'simpleglfw', None, None)

        # make the context current
        glfw.glfwMakeContextCurrent(self.win)

    def main(self):
        glViewport(0, 0, self.width, self.height)
        glEnable(GL_DEPTH_TEST)
        glClearColor(0.5, 0.5, 0.5, 1.0)

wnd = renderwindow()
© www.soinside.com 2019 - 2024. All rights reserved.