python自动打包程序。
如需要选择图标请将"xxx.ico"图片文件添加到和该程序一起的文件夹。
import os
from tkinter import ttk
from tkinter import *
import tkinter as tk
from tkinter import filedialog
class Tv:
def __init__(self):
self.root_windows = Tk()
self.root_windows.geometry('300x300+600+250')
self.root_windows.title("python打包程序")
label1 = Label(self.root_windows, text='请选择文件:')
label1.place(x=10, y=20)
label2 = Label(self.root_windows, text='请选择图标:')
label2.place(x=10, y=50)
self.entry = Entry(self.root_windows, width=22)
self.entry.place(x=90, y=20)
self.t = ttk.Combobox(self.root_windows, width=22)
path = str(os.path.dirname(os.path.abspath(__file__))).replace("\\", "/") # 获取当前程序所在的绝对路径
lists = os.listdir(path) # 循环遍历该程序文件夹的所有文件
numbers = [i for i in lists] # 将文件名称添加到numbers中
self.t['values'] = [i for i in numbers if i[-4:] == '.ico'] + ['No']
self.t.place(x=90, y=52)
# 进度条
progress = ttk.Progressbar(self.root_windows, length=300, mode="indeterminate",
maximum=100, orient=tk.HORIZONTAL) # mode="indeterminate" 进度条一直来回动
progress.place(x=115, y=100, width=100)
progress.start()
label_start = Label(self.root_windows, text='开始打包', font=('楷体', 12))
label_start.place(x=50, y=100)
self.root_windows.update()
label_end = Label(self.root_windows, text='注意:打包软件在打包完成后将文件生成在该文件夹', fg='red', font=('楷体', 9))
label_end.place(x=5, y=250)
button_file = Button(self.root_windows, text='❔', width=2, font=('', 9), command=lambda: [Tv.file_read(self)])
button_file.place(x=250, y=20)
button_sure = Button(self.root_windows, text='确定', width=5, font=('楷体', 12), command=lambda: [Tv.file_sure(self)])
button_sure.place(x=125, y=180)
self.root_windows.mainloop()
def file_read(self):
root_file = tk.Tk()
root_file.withdraw()
filepath = filedialog.askopenfilename()
self.entry.insert('insert', filepath)
root_file.destroy()
def file_sure(self):
filepath = self.entry.get()
photo = self.t.get()
filepath = r"".join(filepath)
photo = r"".join(photo)
cmdpath1 = "pyinstaller -F -w " # 调用cmd窗口
path1 = cmdpath1 + filepath # 进行打包的路径
cmdpath2 = "pyinstaller -F -w -i "
path2 = cmdpath2 + str(photo) + ' ' + filepath
if filepath == '':
label = Label(self.root_windows, text='请输入文件路径', fg='red', font=('楷体', 11))
label.place(x=105, y=135)
label.after(2000, lambda: label.destroy())
else:
if photo == 'No' or photo == '':
os.system(path1)
label = Label(self.root_windows, text='程序打包完成', fg='red', font=('楷体', 11))
label.place(x=105, y=135)
os.system("taskkill /f /im cmd.exe")
# root_windows.after(8000, lambda: root_windows.destroy())
elif photo[-4:] != '.ico':
label = Label(self.root_windows, text='图标格式不正确', fg='red', font=('楷体', 11))
label.place(x=105, y=135)
label.after(2000, lambda: label.destroy())
else:
os.system(path2)
label = Label(self.root_windows, text='程序打包完成', fg='red', font=('楷体', 11))
label.place(x=105, y=135)
os.system("taskkill /f /im cmd.exe")
# root_windows.after(8000, lambda: root_windows.destroy())
if __name__ == "__main__":
app = Tv()