1. 需要实现的功能:
- 实时显示鼠标在屏幕中的(x, y)坐标,方便后续使用 pyautogui 的时候进行定位;
- 打包为.exe文件,直接弹出命令行格式显示实时坐标;
- 使用 PyInstaller 模块进行打包操作;
2. 实现实时显示屏幕坐标的代码
#! python3
import pyautogui, sys, time
print('Press Ctrl-C to quit.')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print("\r" + positionStr, end="", flush=True)
except KeyboardInterrupt:
print('\n')
'''
print 单行刷新参考:https://blog.csdn.net/u013894427/article/details/80305435
flush = True,是 print 的输出不断刷新;
'''
3. 使用 PyInstaller 打包的方法
- 通过 CMD ,输入命令 pyinstaller+ -F + (打包文件 .py 的绝对路径), 其中 -F 表示只生产一个扩展名为 .exe 的可执行文件;
- 按回车,自动生成 .exe 文件
- 需要在单独的虚拟环境中进行打包,不然打包出来的 .exe 文件体积过大,因为牵涉的库太多,所以在虚拟环境中,只保留真正使用上的库;
网友评论