0

Before you close this question this is not like other questions!

my question is how to fix pygame.error: video system not initializedand pygame.init() is already called but without sys.exit()? because I have another tkinter window and I want to exit the pygame window only.

I know this is beginner question but I beginner at pygame and I don't know how to close pygame window without close the whole code. Help

code (with 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()

so the Error at the func run in the bottom when I try to quit pygame by use pygame.quit() and I will have pygame.error.I was trying to use pygame.display.quit() and the same Error ;)

Thx for reading [RIP English]

If you know how to quit the pygame window only please tell me as answer.

EDIT: if I try to do this:

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

it's will be close without error but the tkinter window will be crashed ;)

EDIT 2: if I try to do this:

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

it's will be close without error but the tkinter window will be crashed ;)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Shihab
  • 94
  • 10

1 Answers1

0

I found the solution!

to fix this i want to finish the while loop after closing while pygame.init() is running in the while loop. it's fix that crash by finishing the loop:

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

so the solution steps are:

  1. add pygame.init() to the while loop
  2. finish the while loop after close it by add boolean var to fix crashing in the step one.
  3. and that's it.

thx for reading.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Shihab
  • 94
  • 10