打包工具
- 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()
?