python打包工具
1,https://blog.csdn.net/m0_47017197/article/details/126263560,安装pyinstaller的教程
2,打开cmd,进入setup.py所在的文件夹
3,运行,python setup.py install
4,pip install pyinstaller
5,pyinstaller -F -w operation.py --exclude-module _bootlocale,打包命令
6,pyinstaller -F -w operation.py
cmd进入.py所在的目录:E:\桌面\commandOrder\bin,运行pyinstaller -F -w operation.py打包,会在dist生成.exe包
7,如下是operation.py文件的代码
# -*- coding: utf-8 -*- # module: 指令测试小工具 # author: zhangzx # created: 2023-09-15 14:24:42.590036 # version:1.0 import datetime import json import time import requests # import binn.frame as frame import frame from tkinter import filedialog, messagebox import tkinter as tk import random # 网络请求参数 # 请求头 headers = {'Content-Type': 'application/json'} issueTime = time.strftime('%Y-%m-%d %H:%M:%S') modifyTime = time.strftime('%Y-%m-%d %H:%M:%S') today = datetime.date.today() time = datetime.time(hour=18, minute=0, second=0) today_at_18 = datetime.datetime.combine(today, time) expireTime = today_at_18.strftime("%Y-%m-%d %H:%M:%S") # 获取cookies def getCookies(): rsp = None urlLogin = 'http://' + frame.ipText.get() + '/login' dataLogin = { "userName": "Nick", "password": "password" } try: rsp = requests.post(urlLogin, headers=headers, json=dataLogin) print(urlLogin) print(rsp.cookies) if rsp.status_code != 200: print("请求失败,请检查系统连通性!") messagebox.showinfo("提示", "请求失败,请检查系统连通性!") else: print("请求成功!") except requests.exceptions.InvalidURL: messagebox.showinfo("提示", "请选择测试环境!") return rsp.cookies # 下发指令 def add(): urlAdd = 'http://' + frame.ipText.get() + '/api/new_instruction' dataAdd = { "aprrovalNo": "B123323", "instructionNo": 'A' + str(random.randint(1000000, 9999999)), "product": frame.productText.get("1.0", "end").replace('\n', ''), "asset": frame.assetText.get("1.0", "end").replace('\n', ''), "portfolio": frame.portfolioText.get("1.0", "end").replace('\n', ''), "symbol": frame.symbolText.get("1.0", "end").replace('\n', ''), "tradeSide": frame.tradeSideText.get("1.0", "end").replace('\n', ''), "targetQty": frame.targetQtyText.get("1.0", "end").replace('\n', ''), "targetAmt": frame.targetAmtText.get("1.0", "end").replace('\n', ''), "priceType": frame.priceTypeText.get("1.0", "end").replace('\n', ''), "limitPrice": frame.limitPriceText.get("1.0", "end").replace('\n', ''), "manager": frame.managerText.get("1.0", "end").replace('\n', ''), "createBy": "wangsh", "aprrovalStatus": "check", "issueTime": issueTime, "expireTime": expireTime, "remark": frame.remarkText.get("1.0", "end").replace('\n', '') } rsp = requests.post(urlAdd, headers=headers, json=dataAdd, cookies=getCookies()) if json.loads(rsp.text)["error_no"] == "0": print("下发指令成功!") print("指令号为:" + dataAdd['instructionNo']) messagebox.showinfo("提示", "下发指令成功!") else: print("下发指令失败!") print(json.loads(rsp.text)["error_msg"]) messagebox.showinfo("提示", "下发指令失败!" + json.loads(rsp.text)["error_msg"]) tk.Button(frame.window, text="下发指令", width=10, height=1, command=add).place(x=230, y=320, anchor='nw') # 批量下发指令 # 文件上传 def upload_file(): urlAdd = 'http://' + frame.ipText.get() + '/api/new_instruction' dataBatch = { "aprrovalNo": "B123323", "instructionNo": 'A' + str(random.randint(1000000, 9999999)), "product": "", "asset": "", "portfolio": "", "symbol": "", "tradeSide": "", "targetQty": "", "targetAmt": "", "priceType": "", "limitPrice": "", "manager": "wangsh", "createBy": "wangsh", "aprrovalStatus": "check", "issueTime": issueTime, "expireTime": expireTime, "remark": "" } file_path = filedialog.askopenfilename() # 让用户选择文件 if file_path: with open(file_path, 'r', encoding='utf-8') as f: lines = f.readlines() for i, line in enumerate(lines): values = line.strip().split(',') dataBatch["product"] = values[0] dataBatch["asset"] = values[1] dataBatch["portfolio"] = values[2] dataBatch["symbol"] = values[3] dataBatch["tradeSide"] = values[4] dataBatch["targetQty"] = values[5] dataBatch["targetAmt"] = values[6] dataBatch["priceType"] = values[7] dataBatch["limitPrice"] = values[8] dataBatch["manager"] = values[9] dataBatch["remark"] = values[10] dataBatch["instructionNo"] = 'A' + str(random.randint(1000000, 9999999)) dataBatch["issueTime"] = issueTime print(dataBatch) requests.post(urlAdd, headers=headers, json=dataBatch, cookies=getCookies()) messagebox.showinfo("提示", "下发" + str(len(lines)) + "条指令成功!") tk.Button(frame.window, text="批量指令", width=10, height=1, command=upload_file).place(x=230, y=400, anchor='nw') # 初始化修改指令组件 def modify(): urlModify = 'http://' + frame.ipText.get() + '/api/modify_instruction' dataModify = { "instructionNo": frame.instructionNoText.get("1.0", "end").replace('\n', ''), "targetQty": frame.targetQtyText1.get("1.0", "end").replace('\n', ''), "targetAmt": frame.targetAmtText1.get("1.0", "end").replace('\n', ''), "priceType": frame.priceTypeText1.get("1.0", "end").replace('\n', ''), "limitPrice": frame.limitPriceText1.get("1.0", "end").replace('\n', ''), "manager": frame.managerText1.get("1.0", "end").replace('\n', ''), "createBy": "wangsh", "modifyTime": modifyTime, "remark": frame.remarkText1.get("1.0", "end").replace('\n', '') } rsp = requests.post(urlModify, headers=headers, json=dataModify, cookies=getCookies()) print(rsp.status_code) if json.loads(rsp.text)["error_no"] == "0": print("修改指令成功!") print("指令号为:" + dataModify['instructionNo']) messagebox.showinfo("提示", "修改指令成功!") else: print("修改指令失败!") print(json.loads(rsp.text)["error_msg"]) messagebox.showinfo("提示", "修改指令失败!" + json.loads(rsp.text)["error_msg"]) tk.Button(frame.window, text="修改指令", width=10, height=1, command=modify).place(x=520, y=200, anchor='nw') def cancel(): urlCancel = 'http://' + frame.ipText.get() + '/api/cancel_instruction' dataCancel = { "instructionNo": frame.instructionNoText2.get("1.0", "end").replace('\n', ''), "manager": frame.managerText2.get("1.0", "end").replace('\n', ''), "remark": frame.remarkText2.get("1.0", "end").replace('\n', '') } rsp = requests.post(urlCancel, headers=headers, json=dataCancel, cookies=getCookies()) if json.loads(rsp.text)["error_no"] == "0": print("指令作废成功:" + dataCancel["instructionNo"]) messagebox.showinfo("提示", "指令作废成功!") else: print("指令作废失败!") print(json.loads(rsp.text)["error_msg"]) messagebox.showinfo("提示", "作废指令失败!" + json.loads(rsp.text)["error_msg"]) tk.Button(frame.window, text="作废指令", width=10, height=1, command=cancel).place(x=230, y=540, anchor='nw') def freeze(): urlFreeze = 'http://' + frame.ipText.get() + '/api/freeze_instruction' dataFreeze = { "instructionNo": frame.instructionNoText3.get("1.0", "end").replace('\n', ''), "manager": frame.managerText3.get("1.0", "end").replace('\n', ''), "remark": frame.remarkText3.get("1.0", "end").replace('\n', '') } rsp = requests.post(urlFreeze, headers=headers, json=dataFreeze, cookies=getCookies()) if json.loads(rsp.text)["error_no"] == "0": print("指令冻结成功:" + dataFreeze["instructionNo"]) messagebox.showinfo("提示", "指令冻结成功!") else: print("指令冻结失败!") print(json.loads(rsp.text)["error_msg"]) messagebox.showinfo("提示", "作废冻结失败!" + json.loads(rsp.text)["error_msg"]) tk.Button(frame.window, text="冻结指令", width=10, height=1, command=freeze).place(x=520, y=400, anchor='nw') def unfreeze(): urlUnfreeze = 'http://' + frame.ipText.get() + '/api/unfreeze_instruction' dataUnfreeze = { "instructionNo": frame.instructionNoText4.get("1.0", "end").replace('\n', ''), "manager": frame.managerText4.get("1.0", "end").replace('\n', ''), "remark": frame.remarkText4.get("1.0", "end").replace('\n', '') } rsp = requests.post(urlUnfreeze, headers=headers, json=dataUnfreeze, cookies=getCookies()) if json.loads(rsp.text)["error_no"] == "0": print("指令解冻成功:" + dataUnfreeze["instructionNo"]) messagebox.showinfo("提示", "指令解冻成功!") else: print("指令解冻失败!") print(json.loads(rsp.text)["error_msg"]) messagebox.showinfo("提示", "作废解冻失败!" + json.loads(rsp.text)["error_msg"]) tk.Button(frame.window, text="解冻指令", width=10, height=1, command=unfreeze).place(x=520, y=540, anchor='nw') if __name__ == '__main__': frame.window.mainloop()
8,如下是frame.py文件的代码
# -*- coding: utf-8 -*- # module: 指令测试小工具 # author: zhangzx # created: 2023-09-15 14:24:42.590036 # version:1.0 import tkinter as tk from tkinter.ttk import Combobox # 初始化窗口 window = tk.Tk() window.title("指令操作") window.geometry("700x600") # 初始化下发指令组件 # 获取输入测试环境ip和端口 tk.Label(window, text="选择测试环境:", width=20, height=2).place(x=5, y=5, anchor='nw') # ipText = tk.Text(window, height=1, width=20) ipText = Combobox(window, values=["10.5.4.109:8092", "10.5.5.174:8092"], height=1, width=18, state="readonly") ipText.place(x=200, y=15, anchor='nw') # 产品号 tk.Label(window, text="产品号:", width=20, height=2).place(x=5, y=30, anchor='nw') productText = tk.Text(window, height=1, width=20) productText.place(x=200, y=40, anchor='nw') # 资产单元 tk.Label(window, text="资产单元:", width=20, height=2).place(x=5, y=55, anchor='nw') assetText = tk.Text(window, height=1, width=20) assetText.place(x=200, y=65, anchor='nw') # 组合号 tk.Label(window, text="组合号:", width=20, height=2).place(x=5, y=80, anchor='nw') portfolioText = tk.Text(window, height=1, width=20) portfolioText.place(x=200, y=90, anchor='nw') # 获取代码 tk.Label(window, text="代码:", width=20, height=2).place(x=5, y=105, anchor='nw') symbolText = tk.Text(window, height=1, width=20) symbolText.place(x=200, y=115, anchor='nw') # 交易方向 tk.Label(window, text="交易方向:", width=20, height=2).place(x=5, y=130, anchor='nw') tradeSideText = tk.Text(window, height=1, width=20) tradeSideText.place(x=200, y=140, anchor='nw') # 目标量 tk.Label(window, text="目标量:", width=20, height=2).place(x=5, y=155, anchor='nw') targetQtyText = tk.Text(window, height=1, width=20) targetQtyText.place(x=200, y=165, anchor='nw') # 目标金额 tk.Label(window, text="目标金额:", width=20, height=2).place(x=5, y=180, anchor='nw') targetAmtText = tk.Text(window, height=1, width=20) targetAmtText.place(x=200, y=190, anchor='nw') # 价格类型 tk.Label(window, text="价格类型(0无限价,1有限价):", width=26, height=2).place(x=5, y=205, anchor='nw') priceTypeText = tk.Text(window, height=1, width=20) priceTypeText.place(x=200, y=215, anchor='nw') # 限价 tk.Label(window, text="限价:", width=20, height=2).place(x=5, y=230, anchor='nw') limitPriceText = tk.Text(window, height=1, width=20) limitPriceText.place(x=200, y=240, anchor='nw') # 投资经理 tk.Label(window, text="投资经理:", width=20, height=2).place(x=5, y=255, anchor='nw') managerText = tk.Text(window, height=1, width=20) managerText.place(x=200, y=265, anchor='nw') # 备注 tk.Label(window, text="备注:", width=20, height=2).place(x=5, y=280, anchor='nw') remarkText = tk.Text(window, height=1, width=20) remarkText.place(x=200, y=290, anchor='nw') # 批量下指令 tk.Label(window, text="批量下指令:", width=20, height=2).place(x=5, y=360, anchor='nw') # 初始化修改指令组件 # 指令编号 tk.Label(window, text="指令编号:", width=20, height=2).place(x=400, y=5, anchor='nw') instructionNoText = tk.Text(window, height=1, width=20) instructionNoText.place(x=500, y=15, anchor='nw') # 目标量 tk.Label(window, text="目标量:", width=20, height=2).place(x=400, y=30, anchor='nw') targetQtyText1 = tk.Text(window, height=1, width=20) targetQtyText1.place(x=500, y=40, anchor='nw') # 目标金额 tk.Label(window, text="目标金额:", width=20, height=2).place(x=400, y=55, anchor='nw') targetAmtText1 = tk.Text(window, height=1, width=20) targetAmtText1.place(x=500, y=65, anchor='nw') # 价格类型 tk.Label(window, text="价格类型:", width=20, height=2).place(x=400, y=80, anchor='nw') priceTypeText1 = tk.Text(window, height=1, width=20) priceTypeText1.place(x=500, y=90, anchor='nw') # 限价 tk.Label(window, text="限价:", width=20, height=2).place(x=400, y=105, anchor='nw') limitPriceText1 = tk.Text(window, height=1, width=20) limitPriceText1.place(x=500, y=115, anchor='nw') # 投资经理 tk.Label(window, text="投资经理:", width=20, height=2).place(x=400, y=130, anchor='nw') managerText1 = tk.Text(window, height=1, width=20) managerText1.place(x=500, y=140, anchor='nw') # 备注 tk.Label(window, text="备注:", width=20, height=2).place(x=400, y=155, anchor='nw') remarkText1 = tk.Text(window, height=1, width=20) remarkText1.place(x=500, y=165, anchor='nw') # 初始化作废指令组件 # 指令编号 tk.Label(window, text="指令编号:", width=20, height=2).place(x=5, y=440, anchor='nw') instructionNoText2 = tk.Text(window, height=1, width=20) instructionNoText2.place(x=200, y=450, anchor='nw') # 投资经理 tk.Label(window, text="投资经理:", width=20, height=2).place(x=5, y=465, anchor='nw') managerText2 = tk.Text(window, height=1, width=20) managerText2.place(x=200, y=475, anchor='nw') # 备注 tk.Label(window, text="备注:", width=20, height=2).place(x=5, y=490, anchor='nw') remarkText2 = tk.Text(window, height=1, width=20) remarkText2.place(x=200, y=500, anchor='nw') # 初始化指令冻结 # 指令编号 tk.Label(window, text="指令编号:", width=20, height=2).place(x=400, y=300, anchor='nw') instructionNoText3 = tk.Text(window, height=1, width=20) instructionNoText3.place(x=500, y=310, anchor='nw') # 投资经理 tk.Label(window, text="投资经理:", width=20, height=2).place(x=400, y=325, anchor='nw') managerText3 = tk.Text(window, height=1, width=20) managerText3.place(x=500, y=335, anchor='nw') # 备注 tk.Label(window, text="备注:", width=20, height=2).place(x=400, y=350, anchor='nw') remarkText3 = tk.Text(window, height=1, width=20) remarkText3.place(x=500, y=360, anchor='nw') # 初始化指令解冻 # 指令编号 tk.Label(window, text="指令编号:", width=20, height=2).place(x=400, y=440, anchor='nw') instructionNoText4 = tk.Text(window, height=1, width=20) instructionNoText4.place(x=500, y=450, anchor='nw') # 投资经理 tk.Label(window, text="投资经理:", width=20, height=2).place(x=400, y=465, anchor='nw') managerText4 = tk.Text(window, height=1, width=20) managerText4.place(x=500, y=475, anchor='nw') # 备注 tk.Label(window, text="备注:", width=20, height=2).place(x=400, y=490, anchor='nw') remarkText4 = tk.Text(window, height=1, width=20) remarkText4.place(x=500, y=500, anchor='nw')