脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python开发.exe小工具的详细步骤

Python开发.exe小工具的详细步骤

2021-08-30 00:07潜行100 Python

这篇文章主要介绍了Python开发.exe小工具的详细步骤,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

v1.0.0

完成基础框架、初始功能

背景:为了提高日常工作效率、学习界面工具开发,可以将一些常用的功能集成到一个小的测试工具中,供大家使用。

一、环境

Python3,pyinstall

pyinstall安装:

pip install pyinstaller   (会自动下载future,pywin32,pyinstaller)

或者采用国内镜像 pip install -i https://pypi.douban.com/simple/ pyinstaller(豆瓣源)

二、代码准备,直接上一个可以运行的代码

  1. # coding:utf-8
  2. # @author : csl
  3. # @description : 小工具开发
  4.  
  5. from tkinter import *
  6. import hashlib
  7. import time
  8.  
  9. LOG_LINE_NUM = 0
  10.  
  11. class MY_GUI_SET():
  12. """小工具"""
  13. def __init__(self, init_window_name):
  14. self.init_window_name = init_window_name
  15.  
  16. def set_init_window(self):
  17. self.init_window_name.title("内部测试工具 开发者:潜行100 问题反馈:QQ35643856")
  18. self.init_window_name.geometry("1068x681+10+10")
  19. # init_window["bg"] = "pink"
  20. self.init_window_name.attributes("-alpha", 0.9) # 虚化 值越小虚化程度越高
  21.  
  22. # 标签
  23. self.init_data_label = Label(self.init_window_name, text="待处理数据")
  24. self.init_data_label.grid(row=0, column=0)
  25. self.result_data_label = Label(self.init_window_name, text="输出结果")
  26. self.result_data_label.grid(row=0, column=12)
  27. self.log_label = Label(self.init_window_name, text="日志")
  28. self.log_label.grid(row=12, column=0)
  29. # 文本框
  30. self.init_data_Text = Text(self.init_window_name, width=67, height=35) # 原始数据录入框
  31. self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
  32. self.result_data_Text = Text(self.init_window_name, width=70, height=49) # 处理结果展示
  33. self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
  34. self.log_data_Text = Text(self.init_window_name, width=66, height=9) # 日志框
  35. self.log_data_Text.grid(row=13, column=0, columnspan=10)
  36. # 按钮
  37. self.str_trans_to_md5_button = Button(self.init_window_name, text="字符串转MD5", bg="lightblue", width=10,
  38. command=self.str_trans_to_md5) # 调用内部方法 加()为直接调用
  39. self.str_trans_to_md5_button.grid(row=1, column=11)
  40.  
  41. # 功能函数
  42. def str_trans_to_md5(self):
  43. src = self.init_data_Text.get(1.0, END).strip().replace("\n", "").encode()
  44. # print("src =",src)
  45. if src:
  46. try:
  47. myMd5 = hashlib.md5()
  48. myMd5.update(src)
  49. myMd5_Digest = myMd5.hexdigest()
  50. # print(myMd5_Digest)
  51. # 输出到界面
  52. self.result_data_Text.delete(1.0, END)
  53. self.result_data_Text.insert(1.0, myMd5_Digest)
  54. self.write_log_to_Text("INFO:str_trans_to_md5 success")
  55. except:
  56. self.result_data_Text.delete(1.0, END)
  57. self.result_data_Text.insert(1.0, "字符串转MD5失败")
  58. else:
  59. self.write_log_to_Text("ERROR:str_trans_to_md5 failed")
  60.  
  61. # 获取当前时间
  62. def get_current_time(self):
  63. current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
  64. return current_time
  65.  
  66. # 日志动态打印
  67. def write_log_to_Text(self, logmsg):
  68. global LOG_LINE_NUM
  69. current_time = self.get_current_time()
  70. logmsg_in = str(current_time) + " " + str(logmsg) + "\n" # 换行
  71. if LOG_LINE_NUM <= 7:
  72. self.log_data_Text.insert(END, logmsg_in)
  73. LOG_LINE_NUM = LOG_LINE_NUM + 1
  74. else:
  75. self.log_data_Text.delete(1.0, 2.0)
  76. self.log_data_Text.insert(END, logmsg_in)
  77.  
  78. def gui_start():
  79. init_window = Tk()
  80. MY_GUI_SET(init_window).set_init_window()
  81.  
  82. init_window.mainloop()
  83.  
  84. gui_start()

三、打包.exe文件

如果你的Python安装目录下的Scripts路径是加到了系统环境变量中,那么可以在任意路劲下直接运行如下命令:

pyinstaller.exe -F -icon=F:\testTools D:/pyWorkspace/py_uiTools/ABC_conversion/ABC_conversion.py

如果带-icon参数打包运行时报错,那么可以在你想保存的文件路劲下直接运行如下命令:

pyinstaller.exe -F D:/pyWorkspace/py_uiTools/ABC_conversion/ABC_conversion.py

Python开发.exe小工具的详细步骤

Python开发.exe小工具的详细步骤

后记(打包补充):

1.程序设置自定义图标:pyinstaller -F -i ico_path  py_path

首先需要下载一张正常的ico,不能用直接修改后缀的。

下载图片:    https://www.easyicon.net

图片改为ico:  http://www.ico.la/

输入命令 pyinstaller -F -i "demo.ico" "main.py"

2.报错提示:

pyinstaller -F -i "demo.ico" "main.py" 命令格式一定是先图标路径,再程序路径。

路径最好为英文,没有中文字符;脚本名称里没有特殊字符如 .

使用utf8编码

图标文件必须是正常格式,不能直接更改后缀。

tuble index out of range ---》pyinstaller版本尚未支持python的版本

3.窗口程序

使用 pyinstaller -F -w  -i ico_path  py_path ,这样脚本不会弹出命令窗,前提是用了GUI库.

4.导入模块问题

pyinstaller -F -w  -i  --hidden-import queue ico_path  py_path 加上选项

如:pyinstaller.exe -F -w C:\YOU\py_testTools\ABC_conversion/ABC_conversion.py

Python开发.exe小工具的详细步骤

到此这篇关于Python开发.exe小工具的文章就介绍到这了,更多相关Python小工具内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_35304570/article/details/89424731

延伸 · 阅读

精彩推荐