一次彻底搞定python打包exe

示例程序目录结构

│—— demo.py
|—— res
  |—— fugu.png
  |—— sushiplate.jpg 

实例程序demo.py

import pygame
from pygame.locals import *
from sys import exit
import os,sys

if getattr(sys, 'frozen', False):
    # 打包后获取路径
    cur_path = sys._MEIPASS
else:
    # 没有打包后获取路径
    cur_path = os.path.dirname(__file__)

background_image=os.path.join(cur_path,'res/sushiplate.jpg')
mouse_image=os.path.join(cur_path,'res/fugu.png')

pygame.init()

screen=pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption('Hello pygame')

backgroud=pygame.image.load(background_image).convert()
mouse_cursor=pygame.image.load(mouse_image).convert_alpha()

while True:
    for event in pygame.event.get():
        if event.type== pygame.QUIT:
            exit()

    screen.blit(backgroud,(0,0))

    x,y=pygame.mouse.get_pos()
    x-=mouse_cursor.get_width()/2
    y-=mouse_cursor.get_height()/2
    screen.blit(mouse_cursor,(x,y))

    pygame.display.update()

资源适配打包的相关代码

if getattr(sys, 'frozen', False):
    # 打包后获取路径
    cur_path = sys._MEIPASS
else:
    # 没有打包后获取路径
    cur_path = os.path.dirname(__file__)

background_image=os.path.join(cur_path,'res/sushiplate.jpg')
mouse_image=os.path.join(cur_path,'res/fugu.png')

一键打包命令(将资源文件一同打包)

pyinstaller -F -w --add-data "res/;res/"  demo.py
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值