我有一个使用PyGame(和Python 2.7)构建的小动画代码。我能够使用Pyinstaller构建可执行文件夹,但是当我尝试运行可执行文件时它崩溃了,虽然我能够通过我的终端运行相同的文件(即源代码)而没有任何问题。
import pygame
import exercise
import random as rd
import copy
import pandas as pd
import numpy as np
def animate():
debug_mode=False
done1 = False
dc_x = 30
dc_y = 30
plant_x = 110
plant_y = 30
supp_x = 190
supp_y = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0) #arrows
RED = (255, 0, 0) # Box frames for summary of t, cost and lost sales
GREEN = (0, 255, 0) # Box frames for shipment amounts
DEEPSKYBLUE = (0,191,255) # arc to represent backup WIP inv
DOGERBLUE = (0,191,255) # arc to represent backup FG inv
INDIGO = (75,0,130) # DC/Supp up state
LAVENDER=(238, 130, 238) #DC/Supp down state
OLIVE=(128,128,0) #plant down state
YELLOWGREEN=(173,255,47) #plant up state
DARKYELLOW=(204,204,0)
pygame.init()
screen = pygame.display.set_mode((1000, 600))
clock = pygame.time.Clock()
grand_record={}
############ READ AND PREPARE INITIAL SCREEN for t==0 ################
PLANT_status, SUPP_status, DC_status, disruption, DC_BUp_Plan, Plant_BUp_Plan, Supp_BUp_Plan, Demand, P=exercise.init(debug_mode,False)
grand_record[0]={"DC":DC_status, "Plant":PLANT_status, "Supplier":SUPP_status}
basicFont = pygame.font.SysFont(None, 24)
largeFont = pygame.font.SysFont(None, 35)
pygame.display.set_caption('SCREAM!')
if debug_mode: R=1
else: R= rd.randint(0,25)
scenario_score={"cost":0, "service":0}
while not done1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done1 = True
#if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_DOWN:
# paused()
end_of_one_run=1
total_lost_customer=0
cumulative_inv_holding_cost=0
total_fixed_cost_paid=0
table_output=[]
chart_output=[]
header=["Week","Demand","Upstream shipment", "Shipment from regular DC", "Shipment from backup DC",
"FG Inv at Reg. DC", "FG Inv at B/up. DC", "FG Inv as B/up","LostSales","Start Order for b/up DC", "B/up DC starts","DC Disruption at","Disruption duration", "TotalAvailableWIPSupplyCap",
"Start Order for b/up Supplier", "B/up Supplier starts","Supplier Disruption at","Disruption duration", "Storage Cap Available", "Plant production",
"WIP Inv as B/up","Start Order for b/up Plant", "B/up Plant starts","Plant Disruption at","Disruption duration"]
table_output.append(header)
for t in range(1,P+1): # for the specified number of periods
screen.fill((0, 0, 0))
picture = pygame.image.load('Background.png').convert() #MITwallpaper
screen.blit(picture, [0, 0])
#import pdb; pdb.set_trace()
################################## MAKE CALCULATIONS FOR t>0 ################
grand_record,output=exercise.calculate(t, grand_record[t-1]["Plant"], grand_record[t-1]["Supplier"], grand_record[t-1]["DC"], disruption, DC_BUp_Plan, Plant_BUp_Plan, Supp_BUp_Plan,Demand[R*P+t],grand_record,debug_mode)
chart_output=exercise.prepare_chart_output(chart_output, grand_record)
output_loc=copy.deepcopy(output)
table_output.append(output_loc)
end_of_one_run=end_of_one_run+1
objectives=exercise.calculate_objectives(grand_record, DC_BUp_Plan, Plant_BUp_Plan, Supp_BUp_Plan, t)
lost_cust_text = largeFont.render("Service: "+str(objectives["Service"]), True, WHITE, BLACK)
lost_cust_textRect = lost_cust_text.get_rect()
lost_cust_textRect.centerx = 380
lost_cust_textRect.centery = 420
pygame.draw.rect(screen, RED, (lost_cust_textRect.left - 3, lost_cust_textRect.top - 3, lost_cust_textRect.width + 3, lost_cust_textRect.height + 3))
screen.blit(lost_cust_text, lost_cust_textRect)
inv_hold_text = largeFont.render("Inv.Holding Cost(k$): "+str(round(objectives["InvHoldingCost"]*0.001,1)), True, WHITE, BLACK)
inv_hold_textRect = inv_hold_text.get_rect()
inv_hold_textRect.centerx = 600
inv_hold_textRect.centery = 445
pygame.draw.rect(screen, RED, (inv_hold_textRect.left - 3, inv_hold_textRect.top - 3, inv_hold_textRect.width + 3, inv_hold_textRect.height + 3))
screen.blit(inv_hold_text, inv_hold_textRect)
startup_text = largeFont.render("Startup Cost(k$): "+str(round(objectives["Startup_cost"]*0.001,1)), True, WHITE, BLACK)
startup_textRect = startup_text.get_rect()
startup_textRect.centerx = 840
startup_textRect.centery = 470
pygame.draw.rect(screen, RED, (startup_textRect.left - 3, startup_textRect.top - 3, startup_textRect.width + 3, startup_textRect.height + 3))
screen.blit(startup_text, startup_textRect)
###################################################################################
if t<P:
pygame.display.flip()
clock.tick(8)
else:
#import pdb; pdb.set_trace()
table = pd.DataFrame(table_output)
table.to_excel("Details.xlsx",index=False)
#import pdb; pdb.set_trace()
done1 = True
################################ Want A PARETO Button ###################################
x=500
y=500
w=200
h=50
pygame.draw.rect(screen, DEEPSKYBLUE,(x,y,w,h))
buttontext = basicFont.render("Want A Pareto Solution?", True, BLACK , WHITE )
buttontext_rect=buttontext.get_rect()
buttontext_rect.centerx = (x+(w/2))
buttontext_rect.centery = (y+(h/2))
screen.blit(buttontext, buttontext_rect)
pygame.display.flip()
pygame.time.delay(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exercise.plotcharts(chart_output)
################################################################################
animate()
答案 0 :(得分:1)
我遇到了同样的问题(也是Python 2.7),这对我来说是这样的:
pyinstaller通常会遇到pygame.font.SysFont("some_font", 24)
而是使用pygame.font.Font("your_own_font.ttf", 24)
。
下载字体并将其放在与文件相同的文件夹中,或放在数据文件夹中。
我使用VeraMono并将其放在数据文件夹中,因此我的代码将是
...
basicFont = pygame.font.Font("data/VeraMono", 24)
largeFont = pygame.font.Font("data/VeraMono", 35)
pygame.display.set_caption('SCREAM!')
...
如果您还没有将数据/文件夹添加到项目中,请将以下行添加到您的spec文件中(第一次运行pyinstaller时会立即创建spec文件。它具有与python文件同名。)
# -*- mode: python -*-
block_cipher = None
# This entry is new!
added_files = [
( 'data', 'data'),
]
a = Analysis(['CleanTest.py'],
pathex=['/home/angela/Desktop/Arbeit/VisualTest'],
binaries=[],
datas=added_files, # add the new file to your data tree
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
...
运行pyinstaller <your_file>.spec
,希望它能够正常运作。
答案 1 :(得分:0)
我的猜测是,它在尝试加载背景图片时崩溃了:
foo()
当pyinstaller编译你的代码时,就是它编译的全部内容(包括导入的依赖项)。它创建了dist目录,您可以在其中运行可执行文件,并且您很高兴。
但是,默认情况下,pyinstaller不会引入外部资源。因此,当您运行可执行文件时,它无法找到您的图像,因为它位于不同的目录中。您可以通过手动将图像复制到exe所在的文件夹进行测试,看看它是否有效。
此外,您可以运行exe并将输出推送到调试文件中:
picture = pygame.image.load('Background.png').convert() #MITwallpaper
这样您就可以捕获程序的输出(包括错误消息)。