0、先來張效果圖:

1、安裝pyinsatller
打開命令列視窗,輸入如下指令:pip3 install pyinstaller

我的已經安裝過,所以這樣顯示,
2、使用pyinstaller打包Python程式
首先進入檔案路徑:

輸入以下指令,開始打包:pyinstaller -F -w (-i icofile) filename
- filename表示你的Python程式檔案名
- -w 表示隱藏程式運行時的命令列視窗(不加-w會有黑色視窗)
- 括號內的為可選引數,-i icofile表示給程式加上圖示,圖示必須為.ico格式
- icofile表示圖示的位置,建議直接放在程式檔案夾里面,這樣子打包的時候直接寫檔案名就好
輸入完成,按回車,就會開始自動打包了,第一次打包程序可能比較緩慢,
輸入示例:pyinstaller -F -w happy2021.py

然后我們進入到程式目錄里面會再看到一個名稱為dist目錄,打包好的exe程式就在里面

注意:如果原本的程式中使用到了相應的資源,需要復制到該目錄下,否則程式會無法運行,
雙擊.exe,運行效果:

3、惡搞原始碼:
# 彈窗惡搞
import tkinter as tk
import random
import threading
import time
def dow():
window = tk.Tk()
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
a = random.randrange(0, width)
b = random.randrange(0, height)
window.title('2021新年快樂')
window.geometry("200x50" + "+" + str(a) + "+" + str(b))
tk.Label(window,
text='2021新年快樂!', # 標簽的文字
bg='Red', # 背景顏色
font=('楷體', 17), # 字體和字體大小
width=15, height=2 # 標簽長寬
).pack() # 固定視窗位置
window.mainloop()
threads = []
for i in range(99): # 需要的彈框數量
t = threading.Thread(target=dow)
threads.append(t)
time.sleep(0.1)
threads[i].start()
4、右鍵可以一鍵關閉哦!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/259238.html
標籤:python
