打包工具
- pyinstaller
安装pyinstaller
如果你的网络稳定,通常直接使用下面的命令安装即可:
pip install pyinstaller
当然了,你也可以下载pyinstaller源码包,然后进入包目录执行下面的命令,同样可以安装(前提是需要安装setuptools):
python setup.py install
检查pyinstaller安装成功与否:
只需要执行如下命令其中一个即可:
pyinstaller --version
pyinstaller -v
pyinstaller参数作用
- -F 表示生成单个可执行文件
- -D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)
- -w 表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧
- -c –console, –nowindowed 使用控制台,无界面(默认)
- -p 表示你自己自定义需要加载的类路径,一般情况下用不到
- -i 表示可执行文件的图标
- 其他参数,可以通过
pyinstaller --help
查看
开始打包
进入python需要打包的脚本所在目录,然后执行下面的命令即可:
pyinstaller -F -i favicon.ico xxx.py
打包结果
打包完成后,进入到当前目录下,会发现多了__pycache__、build、dist、nhdz.spec这四个文件夹或者文件,其中打包好的exe应用在dist目录下面,进入即可看到,可以把他拷贝到其他地方直接使用,如下图所示,是打包完成后的目录:
执行exe应用
因为是exe应用,是可执行文件了,所以直接双击运行即可,运行效果如下图所示:
到这里,exe文件就已经生算是打包完成,并且可以运行了,如果你想在其他平台运行,只需要拷贝dist下面的文件即可
ICO图标制作
前面需要用到ICO图标,大家可以网上搜索“ICO 在线生成”,可以直接点击ICO图标制作在上面制作、然后保存也行
测试程序源码
# -*- coding: utf-8 -*-
# @Time : 2019/07/14 19:47
# @Author : Liu
# @File : exe.pyimport random
import timedef enter_stake(current_money):'''输入小于结余的赌资及翻倍率,未考虑输入type错误的情况'''stake = int(input('How much you wanna bet?(such as 1000):'))rate = int(input("What multiplier do you want?你想翻几倍?(such as 2):"))small_compare = current_money < stake * ratewhile small_compare == True:stake = int(input('You has not so much money ${}!How much you wanna bet?(such as 1000):'.format(stake * rate)))rate = int(input("What multiplier do you want?你想翻几倍?(such as 2):"))small_compare = current_money < stake * ratereturn stake,ratedef roll_dice(times = 3):'''摇骰子'''print('<<<<<<<<<< Roll The Dice! >>>>>>>>>>')points_list = []while times > 0:number = random.randrange(1,7)points_list.append(number)times -= 1return points_listdef roll_result(total):'''判断是大是小'''is_big = 11 <= total <= 18is_small = 3 <= total <= 10if is_small:return 'Small'elif is_big:return 'Big'def settlement(boo,points_list,current_money,stake = 1000,rate = 1):'''结余'''increase = stake * rateif boo:current_money += increaseprint('The points are ' + str(points_list) + ' .You win!')print('You gained $' + str(increase) + '.You have $' + str(current_money) + ' now.' )else:current_money -= increaseprint('The points are ' + str(points_list) + ' .You lose!')print('You lost $' + str(increase) + '.You have $' + str(current_money) + ' now.' )return current_moneydef sleep_second(seconds=1):'''休眠'''time.sleep(seconds)# 开始游戏
def start_game():'''开始猜大小的游戏'''current_money = 1000print('You have ${} now.'.format(current_money))sleep_second()while current_money > 0:print('<<<<<<<<<<<<<<<<<<<< Game Starts! >>>>>>>>>>>>>>>>>>>>')your_choice = input('Big or Small: ')choices = ['Big','Small']if your_choice in choices:stake,rate = enter_stake(current_money)points_list = roll_dice()total = sum(points_list)actual_result = roll_result(total)boo = your_choice == actual_resultcurrent_money = settlement(boo,points_list,current_money,stake,rate)else:print('Invalid input!')else:sleep_second()print('Game Over!')sleep_second(2)if __name__ == '__main__':start_game()