pygame.error: 视频系统未初始化。 pygame.init() 已经调用但没有 sys.exit()?

时间:2021-01-20 17:52:48

标签: python python-3.x tkinter pygame

在关闭这个问题之前,这与其他问题不同!

我的问题是如何修复 pygame.error: video system not initialized 并且 pygame.init() 已经被调用但没有 sys.exit()? 因为我有另一个 tkinter 窗口,我想仅退出 pygame 窗口

我知道这是初学者的问题,但我是 pygame 的初学者,我不知道如何在不关闭整个代码的情况下关闭 pygame 窗口。帮助

代码(带有 pygame.Error):

class editor:
    def __init__(self):
        new_frm.pack_forget()
        welc.pack_forget()
        new_btn.pack_forget()

        ## FRAMES ##
        self.rot = Frame(root,width=root.winfo_screenwidth(),height=80,bg="#282828")
        self.rot.pack()

        # __|ADD A LAYOUT FOR ROOT|__ #
           # Resolution #
        # X #
        Label(self.rot,text=" X : ",bg="#282828",fg="white",borderwidth=0,font=("arial",17)).place(x=8,y=20)
        self.ent_x = Entry(self.rot,borderwidth=0,bg="#323232",width=5,fg="orange",font=("arial",20))
        self.ent_x.place(x=50,y=20,height=30)
        self.ent_x.insert(END,"600")
        # Y #
        Label(self.rot,text=" Y : ",bg="#282828",fg="white",borderwidth=0,font=("arial",17)).place(x=147,y=20)
        self.ent_y = Entry(self.rot,borderwidth=0,bg="#323232",width=5,fg="orange",font=("arial",20))
        self.ent_y.place(x=200,y=20,height=30)
        self.ent_y.insert(END,"400")
        # RUN #
        run_load = Image.open(os.path.join(dirname, 'Data/Icons/RUN.png'))
        width, height = run_load.size
        run_load = run_load.resize((int(width/16),int(height/16)), Image.ANTIALIAS)
        run_render = ImageTk.PhotoImage(run_load)
        self.run_btn = Button(self.rot,image=run_render,bg="#282828",borderwidth=0,activebackground="orange",command=lambda: self.run(int(self.ent_x.get()),int(self.ent_y.get())))
        self.run_btn.place(relx=1,x=-60,y=20)
        # To Fix if "run" image not loading #
        canvas = tkinter.Canvas(master)
        canvas.grid(row=0,column=0)
    
    def run(self, x: int, y: int):
        pygame.init()
        size = width, height = x, y
        screen = pygame.display.set_mode(size)
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                   # Here is the Error :
                    pygame.quit()

所以当我尝试使用 run 退出 pygame 时,底部 func pygame.quit() 处的错误,我将有 pygame.error。我试图使用 { {1}} 和相同的错误 ;)

感谢阅读 [RIP 英文]

如果您知道如何仅退出 pygame 窗口,请告诉我作为答案。

编辑: 如果我尝试这样做:

pygame.display.quit()

它将无错误地关闭,但 tkinter 窗口将崩溃;)

编辑 2: 如果我尝试这样做:

        while 1:
            pygame.init()
            ev = pygame.event.poll() 
            if ev.type == pygame.QUIT:
                pygame.display.quit()

它将无错误地关闭,但 tkinter 窗口将崩溃;)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案!

为了解决这个问题,我想在关闭 while pygame.init() 在 while 循环中运行后完成 while 循环。它通过完成循环来修复崩溃:

g = True
while g:
    pygame.init()
    if pygame.event.poll().type == pygame.QUIT:
        pygame.display.quit()
        g = False

所以解决步骤是:

  1. pygame.init() 添加到 while 循环
  2. 在关闭 while 循环后通过添加布尔变量来完成第一步中的崩溃。
  3. 就是这样。

感谢阅读。

相关问题