2

I want to be able to convert .py to .exe for python 3 pygame 1.9.4 projects (on my Windows 10 computer), and I have successfully converted some files by using auto py to exe (one file, console based): https://nitratine.net/blog/post/auto-py-to-exe/. For example, I tried to convert the code that is quite far up on this site: http://openbookproject.net/thinkcs/python/english3e/pygame.html. That is:

import pygame

def main():
    """ Set up the game and run the main game loop """
    pygame.init()      # Prepare the pygame module for use
    surface_sz = 480   # Desired physical surface size, in pixels.

    # Create surface of (width, height), and its window.
    main_surface = pygame.display.set_mode((surface_sz, surface_sz))

    # Set up some data to describe a small rectangle and its color
    small_rect = (300, 200, 150, 90)
    some_color = (255, 0, 0)        # A color is a mix of (Red, Green, Blue)

    while True:
        ev = pygame.event.poll()    # Look for any event
        if ev.type == pygame.QUIT:  # Window close button clicked?
            break                   #   ... leave game loop

        # Update your game objects and data structures here...

        # We draw everything from scratch on each frame.
        # So first fill everything with the background color
        main_surface.fill((0, 200, 255))

        # Overpaint a smaller rectangle on the main surface
        main_surface.fill(some_color, small_rect)

        # Now the surface is ready, tell pygame to display it!
        pygame.display.flip()

    pygame.quit()     # Once we leave the loop, close the window.

main()

The .exe file ran as it should when opened. However, some programs that I write are only partially functioning after the conversion. One such program would be:

import pygame as pg

def main():
    pg.init()
    pg.display.set_caption('Some Caption')
    screen = pg.display.set_mode((640, 480))

    afont = pg.font.SysFont("some_font", 32)
    done=False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
        screen.fill(0)
        sometext = afont.render("Amazing text"
                .format(0, 0), True, (250,200,250))
        screen.blit(sometext, (10, 50))
        pg.display.flip()


if __name__.endswith('__main__'):
    main()
    pg.quit()

After converted to exe, when I open it, some text about pygame appears and also the screen that is meant to pop up, showing the caption which I've set ('Some Caption'). The screen which popped up is however black and closes after a while, not showing what it should. However, I did get my exe file without any errors (which I could see anyway in the Output of the GUI which I used for the conversion i.e. auto py to exe). So in search of solutions I found this website: https://nitratine.net/blog/post/issues-when-using-auto-py-to-exe/.

There were alot about error messages and general information but the thing I saw about program that opens but not doing what it should before closing was:

The Terminal Just Opens and Closes But There Are No Errors If you double click to run your Python script, what happens? Does it open and close also? That means this tool has done it's job correctly and the script is finishing just like it should.

You most likely think the output should stay visible because you are always using IDLE or an IDE and that's what those tools do. Add a statement like input() at the end of your script to block execution and wait for you to press enter before closing.

This, is not the solution for my problem since my program works if I double click on the python script. However, I think that it has a good point i.e that there might be something in my code that do not handle the conversion well, which is probable since the code from http://openbookproject.net/thinkcs/python/english3e/pygame.html did work after being converted to .exe when the same method was used.

My question is therefore, what is it that make my code not work after the conversion from .py to .exe?, and in that case, in what way might it be fixable?. The knowledge of what works and not when it comes to the converting .py to .exe would enable more programs to function correctly when they are .exe.

Thank you

0 Answers0