1

I converted a pygame file to exe with cx_Freeze, and the process went fine. However, when I click on the executable, it opens, then closes immediately. I'm wondering if it's a problem with either main file or my setup.py?

Main:

import pygame
pygame.init()

white = 255,255,255 #This block defines all the colors in (R,G,B) format
black = 0,0,0
green = 0,255,0
blue = 0,76,153
red = 255,0,0
orange = 255,128,0
yellow = 255,255,0
indigo = 204,0,204
pink = 255,51,255

score_1=0
score_2=0
score_3=0
score_4=0
score_5=0
score_6=0
score_7=0

height = 1366 #The height and width of our window
width = 768
window = pygame.display.set_mode((height,width)) 
pygame.display.set_caption("Score") #Edit the text between quotes to change window title

title_font = pygame.font.SysFont('Comic Sans MS', 70)   #This font is for title in (font,size) format
button_font = pygame.font.SysFont('Comic Sans MS', 12)

title = title_font.render("Chem Rush", True, (black))

clock = pygame.time.Clock()
crashed = False

flask_img_right = pygame.image.load('flask.jpg').convert() #This block is for loading all images
flask_img_left = pygame.image.load('flask1.jpg').convert()
danger_sign = pygame.images.load('danger.jpg').convert()

def flask_right(x_fr,y_fr):
    window.blit(flask_img_right,(x_fr,y_fr))
x_fr = (1000)
y_fr = (200)

def flask_left(x_fl,y_fl):
    window.blit(flask_img_left,(x_fl,y_fl))
x_fl = (100)
y_fl = (200)

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: "+str(count), True, black)
    gameDisplay.blit(text,(0,0))


while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
    #Increasing the first number moves text to the right
    #Increasing the second number moves text lower
    #Assume this for everything below unless otherwise stated

    window.fill(white)
    flask_right(x_fr,y_fr)
    flask_left (x_fl, y_fl)
    window.blit(title,(500,50))   

    mouse = pygame.mouse.get_pos()

    b1_surf, b1_rect = text_objects("", button_font)
    b1_rect.center = ((550),(220))
    window.blit(b1_surf, b1_rect)
    pygame.draw.rect(window, blue, (500,200,100,50))
    b1_text, b1t_rect = text_objects("Team 1", button_font)
    b1t_rect.center = ((550),(220))
    window.blit(b1_text, b1t_rect)
    pygame.draw.rect(window,white,(700,200,50,50))
    new_score1 = button_font.render(str(score_1), True, (black))
    window.blit(new_score1,(725,225))
    if event.type == pygame.MOUSEBUTTONDOWN:
        if 500+100>mouse[0]>500 and 200+50>mouse[1]>200:
            if event.button == 1:   #Left mouse click
                score_1+=1
                window.blit(new_score1,(725,225))
            elif event.button == 3: #Right mouse click
                score_1-=1
                window.blit(new_score1,(725,225))

    b2_surf, b2_rect = text_objects("",button_font)
    b2_rect.center = ((550,270))
    window.blit(b2_surf, b2_rect)
    pygame.draw.rect(window, red,(500,250,100,50))
    b2_text, b2t_rect = text_objects("Team 2", button_font)
    b2t_rect.center = ((550,270))
    window.blit(b2_text, b2t_rect)
    pygame.draw.rect(window,white,(700,250,50,50))
    new_score2 = button_font.render(str(score_2), True, (black))
    window.blit(new_score2,(725,275))
    if event.type == pygame.MOUSEBUTTONDOWN:
        if 500+100>mouse[0]>500 and 250+50>mouse[1]>250:
            if event.button == 1:
                score_2+=1
                window.blit(new_score2,(725,275))
            elif event.button == 3:
                score_2-=1
                window.blit(new_score2,(725,275))

    b3_surf, b3_rect = text_objects("",button_font)
    b3_rect.center = ((550,320))
    window.blit(b3_surf, b3_rect)
    pygame.draw.rect(window, orange,(500,300,100,50))
    b3_text, b3t_rect = text_objects("Team 3", button_font)
    b3t_rect.center = ((550,320))
    window.blit(b3_text, b3t_rect)
    pygame.draw.rect(window,white,(700,300,50,50))
    new_score3 = button_font.render(str(score_3), True, (black))
    window.blit(new_score3,(725,325))
    if event.type == pygame.MOUSEBUTTONDOWN:
        if 500+100>mouse[0]>500 and 300+50>mouse[1]>300:
            if event.button == 1:
                score_3+=1
                window.blit(new_score3,(725,325))
            elif event.button == 3:
                score_3-=1
                window.blit(new_score3,(725,325))

    b4_surf, b4_rect = text_objects("",button_font)
    b4_rect.center = ((550,370))
    window.blit(b4_surf, b4_rect)
    pygame.draw.rect(window, yellow,(500,350,100,50))
    b4_text, b4t_rect = text_objects("Team 4", button_font)
    b4t_rect.center = ((550,370))
    window.blit(b4_text, b4t_rect)
    pygame.draw.rect(window,white,(700,350,50,50))
    new_score4 = button_font.render(str(score_4), True, (black))
    window.blit(new_score4,(725,375))
    if event.type == pygame.MOUSEBUTTONDOWN:
        if 500+100>mouse[0]>500 and 350+50>mouse[1]>350:
            if event.button == 1:
                score_4+=1
                window.blit(new_score4,(725,375))
            elif event.button == 3:
                score_4-=1
                window.blit(new_score4,(725,375))

    b5_surf, b5_rect = text_objects("",button_font)
    b5_rect.center = ((550,420))
    window.blit(b5_surf, b5_rect)
    pygame.draw.rect(window, indigo,(500,400,100,50))
    b5_text, b5t_rect = text_objects("Team 5", button_font)
    b5t_rect.center = ((550,420))
    window.blit(b5_text, b5t_rect)
    pygame.draw.rect(window,white,(700,400,50,50))
    new_score5 = button_font.render(str(score_5), True, (black))
    window.blit(new_score5,(725,425))
    if event.type == pygame.MOUSEBUTTONDOWN:
        if 500+100>mouse[0]>500 and 400+50>mouse[1]>400:
            if event.button == 1:
                score_5+=1
                window.blit(new_score5,(725,425))
            elif event.button == 3:
                score_5-=1
                window.blit(new_score5,(725,425))

    b6_surf, b6_rect = text_objects("",button_font)
    b6_rect.center = ((550,470))
    window.blit(b6_surf, b6_rect)
    pygame.draw.rect(window, green,(500,450,100,50))
    b6_text, b6t_rect = text_objects("Team 6", button_font)
    b6t_rect.center = ((550,470))
    window.blit(b6_text, b6t_rect)
    pygame.draw.rect(window,white,(700,450,50,50))
    new_score6 = button_font.render(str(score_6), True, (black))
    window.blit(new_score6,(725,475))
    if event.type == pygame.MOUSEBUTTONDOWN:
        if 500+100>mouse[0]>500 and 450+50>mouse[1]>450:
            if event.button == 1:
                score_6+=1
                window.blit(new_score6,(725,475))
            elif event.button == 3:
                score_6-=1
                window.blit(new_score6,(725,475))

    b7_surf, b7_rect = text_objects("",button_font)
    b7_rect.center = ((550,520))
    window.blit(b7_surf, b7_rect)
    pygame.draw.rect(window, pink,(500,500,100,50))
    b7_text, b7t_rect = text_objects("Team 7", button_font)
    b7t_rect.center = ((550,520))
    window.blit(b7_text, b7t_rect)
    pygame.draw.rect(window,white,(700,500,50,50))
    new_score7 = button_font.render(str(score_7), True, (black))
    window.blit(new_score7,(725,525))
    if event.type == pygame.MOUSEBUTTONDOWN:
        if 500+100>mouse[0]>500 and 500+50>mouse[1]>500:
            if event.button == 1:
                score_7+=1
                window.blit(new_score7,(725,525))
            elif event.button == 3:
                score_7-=1
                window.blit(new_score7,(725,525))

    pygame.display.update()
    clock.tick(10)

pygame.quit()
quit()
end_command = input("any: ")

setup script:

from cx_Freeze import setup, Executable

setup(name = "Score Board" ,
      version = "0.1" ,
      description = "" ,
      executables = [Executable("Score_Keeper.py")])

For my program, I only imported pygame. I also only used 2 of the 3 images I loaded in. When I clicked on the exe, it would show a command terminal, a black window, and some text on the terminal. However, it would close before I could read it.

edit:
i fixed it, if you want to know how, just check the comments to this post.

Zayed Azam
  • 83
  • 1
  • 7
  • 3
    run it manually in console/termina/cmd.exe to see if you get error message. Maybe it needs other modules/libraries to work. In console/terminal/cmd.exe you should see more informations. – furas Jan 05 '18 at 15:44
  • thanks, turns out it couldn't open the flask.jpg image. is there any way to fix this? all images are in the same folder as the main program and setup. – Zayed Azam Jan 05 '18 at 15:50
  • 2
    you may have to add image to resources in `setup` - as I remember it creates ZIP file with extension .exe and whe you run .exe it decompresses all files to temporaty file. You can also use method to find folder with application and apped to image name to use only full path. – furas Jan 05 '18 at 16:03
  • i fixed it, all i did was not load the 'danger' image, and put the two flask images in the same directory as the exe! – Zayed Azam Jan 05 '18 at 16:06
  • 1
    @ZayedAzam Do you know how to do this with setup script or did you do it manually? If you want to know how to include files with setup script tell me and I'll make an answer. – Xantium Jan 06 '18 at 01:27
  • @Simon sorry about the late reply, yeah all I did was just put them in the same folder as the .exe. – Zayed Azam Jan 13 '18 at 00:46
  • No problem. Shall I do an answer that uses the setup script only (ie no manual copying)? Unless you want to do an answer yourself, – Xantium Jan 13 '18 at 00:50
  • @Simon no need, but if you want to, sure! – Zayed Azam Jan 13 '18 at 00:53

1 Answers1

0

From the comments above you have concluded that the images flask.jpg, flask1.jpg and danger.jpg are missing. Although copying them into your build folder is fine and everything will work, if you want to create an installer (IE bdist_msi or bdist_mac) this will not work. You need to tell the setup script to include these three files.

This is easy enough you just need to use the include_files argument. Unfortunately the docs don't tell us much on this versatile function but it can be done (as described here) like this:

from cx_Freeze import setup, Executable

files = ["flask.jpg", "flask1.jpg", "danger.jpg"] 
setup(name = "Score Board" ,
    version = "0.1" ,
    description = "" ,
    options = {'build_exe': {'include_files':files}}
    executables = [Executable("Score_Keeper.py")])
Xantium
  • 11,201
  • 10
  • 62
  • 89