我决定将我的pygame项目从.py格式构建为exe.当我试图运行脚本时,我遇到了一个错误



我的项目有很多文件,这个应用程序从start.py脚本开始。当我运行这个脚本时,我得到一个";导入错误";。我使用pyinstaller模块将我的项目构建为exe。

代码

import json
import os
import pygame as pg
from about import About
from code import App

class Menu:
def __init__(self):
pg.init()
pg.mixer.init()
self.file_dir = os.path.dirname(__file__)
self.json_conf_dir = os.path.join(self.file_dir)
self.font_dir = os.path.join(self.file_dir)
self.img_dir = os.path.join(self.file_dir)
with open(os.path.join(self.json_conf_dir, "file_paths.json")) as file_paths:
self.data = json.load(file_paths)
self.font_file = os.path.join(self.font_dir, self.data["fonts"][1])
self.background_img = pg.image.load(os.path.join(self.img_dir, self.data["images"]["menu_imgs"]["background"]))
self.logo_img = pg.image.load(os.path.join(self.img_dir, self.data["images"]["icon"]))
self.height, self.width = 1000, 600
self.screen = pg.display.set_mode((self.height, self.width))
pg.display.set_caption("Battle on Tower {MENU}")
pg.display.set_icon(self.logo_img)
self.colors = [(0, 0, 0), (255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255)]
self.menu = True
self.menu_cycle()
def menu_cycle(self):
while self.menu:
for event in pg.event.get():
if event.type == pg.QUIT:
self.menu = False
self.button_press_event(event)
self.update_screen()
def text_render(self, text, font, size, color, x, y):
font = pg.font.Font(font, size)
text = font.render(text, True, color)
self.screen.blit(text, (x, y))
def draw_buttons(self):
global play_btn, quit_btn, about_btn
play_btn = pg.draw.rect(self.screen, self.colors[-2], (400, 200, 200, 75))
quit_btn = pg.draw.rect(self.screen, self.colors[2], (400, 300, 200, 75))
about_btn = pg.draw.rect(self.screen, self.colors[1], (400, 400, 200, 75))
self.text_render("Play", self.font_file, 60, self.colors[0], 420, 210)
self.text_render("Quit", self.font_file, 60, self.colors[0], 420, 310)
self.text_render("About", self.font_file, 50, self.colors[0], 400, 420)
pg.draw.rect(self.screen, self.colors[-1], (400, 200, 200, 75), 3)
pg.draw.rect(self.screen, self.colors[-1], (400, 300, 200, 75), 3)
pg.draw.rect(self.screen, self.colors[-1], (400, 400, 200, 75), 3)
def button_press_event(self, event):
global play_btn, quit_btn, about_btn
if event.type == pg.MOUSEBUTTONUP:
mouse_pos = pg.mouse.get_pos()
if play_btn.collidepoint(mouse_pos):
App()
self.menu = False
if quit_btn.collidepoint(mouse_pos):
self.menu = False
if about_btn.collidepoint(mouse_pos):
About()
self.menu = False
def update_screen(self):
self.screen.blit(self.background_img, (0, 0))
self.draw_buttons()
pg.display.update()

if __name__ == "__main__":
menu = Menu()

Class方法,其中从其他脚本创建类的无形内容:

def button_press_event(self, event):
global play_btn, quit_btn, about_btn
if event.type == pg.MOUSEBUTTONUP:
mouse_pos = pg.mouse.get_pos()
if play_btn.collidepoint(mouse_pos):
App()
self.menu = False
if quit_btn.collidepoint(mouse_pos):
self.menu = False
if about_btn.collidepoint(mouse_pos):
About()
self.menu = False

错误的照片在此处输入图像描述

链接到我的GitHub与我的项目的完整版本

pyinstaller命令:

pyinstaller --windowed --onefile --add-data "data;." --add-data "about.py;." --add-data "code.py;." --add-data "file_paths.json;." --icon=icon.ico menu.py

这意味着"应用程序";不是";代码";可能是错误的大写字母或名称,或者你没有";代码";库,或库的代码问题。

相关内容

  • 没有找到相关文章

最新更新