如何修复在 Mac 上使用 PyOpenGL 时的“分段错误”

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

OpenGL 初学者在这里。我遵循了 OpenGL 的 Python 教程,我的脚本最初运行正常,按预期显示了一个窗口。但是,后续运行会导致分段错误 (

zsh: segmentation fault /usr/local/bin/python3
),但没有任何 Python 错误。我测试了另一个 Python 程序(斐波那契),它运行得很好。有人可以帮忙吗?这是我的脚本:

import pygame as pg
from OpenGL.GL import *

class App():
    def __init__(self):
        # init python 
        pg.init()
        self.clock = pg.time.Clock()
        # init opengl
        glClearColor(0.1, 0.2, 0.2, 1)
        self.mainLoop()
    
    def mainLoop(self):
        running = True
        while running:
            # check events
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    running = False
            
            # refresh
            glClear(GL_COLOR_BUFFER_BIT)
            pg.display

            # timing
            self.clock.tick(60)
        self.quit()
    
    def quit(self):
        pg.quit()

if __name__ == "__main__":
    myApp = App()
python opengl pyopengl
1个回答
0
投票

您必须使用

pygame.display.set_mode
:

创建 OpenGL 显示
import pygame as pg
from OpenGL.GL import *

class App():
    def __init__(self):
        pg.init()
        pg.display.set_mode((800, 600), pg.OPENGL) # create OpenGL display
        self.clock = pg.time.Clock()
        
        glClearColor(0.1, 0.2, 0.2, 1)
        self.mainLoop()
    
    def mainLoop(self):
        running = True
        while running:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    running = False

            # draw 
            glClear(GL_COLOR_BUFFER_BIT)
            # [...]

            # update display
            pg.display.flip()
            self.clock.tick(60)
        self.quit()
    
    def quit(self):
        pg.quit()

if __name__ == "__main__":
    myApp = App()
© www.soinside.com 2019 - 2024. All rights reserved.