gpt4 book ai didi

python - 如何解决错误: AttributeError: module 'pyglfw' has no attribute 'pyglfwInit'

转载 作者:行者123 更新时间:2023-12-04 18:44:09 27 4
gpt4 key购买 nike

我想用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 不匹配。我不知道如何解决

最佳答案

方法名不是 glfwInit , 是 init .这也适用于其他方法。 (window_hintcreate_windowmake_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()

关于python - 如何解决错误: AttributeError: module 'pyglfw' has no attribute 'pyglfwInit' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61381628/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com