我使用cx_Freeze将pygame文件转换为exe,并且过程很顺利。但是,当我单击可执行文件时,它会打开,然后立即关闭。我想知道主文件或我的setup.py是否有问题?
主:
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: ")
设置脚本:
from cx_Freeze import setup, Executable
setup(name = "Score Board" ,
version = "0.1" ,
description = "" ,
executables = [Executable("Score_Keeper.py")])
对于我的程序,我只导入了pygame。我也只使用了我加载的3个图像中的2个。当我点击exe时,它会显示命令终端,黑色窗口和终端上的一些文本。但是,在我阅读它之前它会关闭。
编辑:
我修好了,如果你想知道怎么做,请查看这篇文章的评论。
答案 0 :(得分:0)
从上面的评论中你得出结论,图像flask.jpg,flask1.jpg和danger.jpg都缺失了。虽然将它们复制到您的构建文件夹中很好,一切都会正常工作,如果您想创建一个安装程序(IE bdist_msi
或bdist_mac
),这将无法正常工作。您需要告诉安装脚本包含这三个文件。
这很简单,你只需要使用include_files
参数。不幸的是,文档并没有告诉我们这个多功能的功能,但可以这样做(如here所述):
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")])