[Python]利用tkinter製作一個Python的exe簡單打包軟體
阿新 • • 發佈:2021-02-05
製作緣由
一方面,想寫一個較簡單、內含功能較多的tkinter視窗,以便將來給其他程式作模板用。另一方面,用命令列打包exe太麻煩了。
介面預覽
若檔案地址是手動輸入,則點選‘確認’後點擊‘打包’。
若檔案地址是點選‘選擇檔案’選定的,則可以直接點選‘打包’。
程式碼演示
寫完後只是粗略地測試了一下,可能會有一點bug。如果有bug及程式碼修改方式希望能告知一下,謝謝!!!!
另外,如果有人知道如何更快更好地進行控制元件排版的話,請再評論區告知,謝謝!!!!
from tkinter import Tk, Button, filedialog, Entry
import os
class Installer:
def __init__(self):
self.WIN = Tk()
self.WIN.title('installer') # 視窗名
self.WIN.geometry('400x104+568+380') # 我的電腦螢幕為1536*864
self.py_path = '' # Python檔案地址
self.ico_path = '' # 圖示檔案地址
# 建立控制元件
self.py_btn = Button(self. WIN, text='選擇py檔案', width=10, command=self.event_py)
self.ico_btn = Button(self.WIN, text='選擇ico檔案', width=10, command=self.event_ico)
self.change_btn = Button(self.WIN, text='打包', width=15, command=self.event_change)
self.qr_btn = Button(self.WIN, text='確認', width=15, command= self.event_qr)
self.py_entry = Entry(self.WIN, show=None, width=40)
self.ico_entry = Entry(self.WIN, show=None, width=40)
# 部署控制元件
self.py_btn.place(x=305, y=5)
self.ico_btn.place(x=305, y=35)
self.change_btn.place(x=235, y=70)
self.qr_btn.place(x=40, y=70)
self.py_entry.place(x=10, y=10)
self.ico_entry.place(x=10, y=40)
# 進入視窗迴圈
self.WIN.mainloop()
def event_py(self): # 點選按鈕‘選擇py檔案’事件
self.py_path = filedialog.askopenfilename(filetypes=[('Python File', '*.py')])
self.py_entry.select_clear()
self.py_entry.insert(0, self.py_path)
def event_ico(self): # 點選按鈕‘選擇ico檔案’事件
self.ico_path = filedialog.askopenfilename(filetypes=[('Icon File', '*.ico')])
self.ico_entry.select_clear()
self.ico_entry.insert(0, self.ico_path)
def event_change(self): # 點選按鈕‘打包’事件
if self.ico_path == '':
if not os.path.exists(self.py_path.rstrip('.py')):
os.mkdir(self.py_path.rstrip('.py'))
cmd1 = self.py_path[0] + ': & cd ' + self.py_path.rstrip('.py') + \
' & pyinstaller -D -w ' + self.py_path
os.system(cmd1)
else:
if not os.path.exists(self.py_path.rstrip('.py')):
os.mkdir(self.py_path.rstrip('.py'))
cmd1 = self.py_path[0] + ': & cd ' + self.py_path.rstrip('.py') + \
' & pyinstaller -D -w -i ' + self.ico_path + ' ' + self.py_path
os.system(cmd1)
def event_qr(self): # 點選按鈕‘確認’事件
self.py_path = str(self.py_entry.get())
self.ico_path = str(self.ico_entry.get())
if __name__ == '__main__':
Install = Installer()