我正在用Python构建一个截图程序,以完成一项工作。我的计算机上下载了python,但是需要运行该程序的计算机上没有python,因此我需要将脚本制作成.exe文件。我从命令提示符处都使用了pyinstaller和auto-py-to-exe,并获得了完全相同的结果。该脚本可以正常编译,没有错误,并创建了内置目录。 .pyc文件在目录中可以完美地运行该程序,但是.exe无法运行。
代码本身由tkinter gui,Imagegrab屏幕快照和类似PyQt5 snippingtool的函数组成。
当我尝试运行.exe文件时,它立即关闭并在图片中显示错误。我试过用--debug运行pyinstaller,甚至将input()添加到脚本的末尾,希望能捕获命令窗口中的错误,但是什么也没显示。我也尝试使用cx-freeze进行编译,但是由于需要Microsoft Visual Studios C ++而出现错误,由于管理块而无法下载。 def snipping:脚本的一部分来自其他人的脚本,因此如果错误来自那里,我不太了解它是如何工作的。除此之外,感觉到我已经尽一切努力使可执行文件得以运行,但是感觉到某个地方出现了用户错误。如果可以的话,我正在运行Windows 10,另一台计算机也将运行。
from tkinter import *
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
import tkinter as tk
import numpy as np
import cv2
from PIL import ImageGrab
#Functions and Variables------------------------------------------------------
running = False
frequency = 2
x1 = 0; y1 = 0; x2 = 0; y2 = 0
path=""
def scanning():
global running, frequency, path
if running:
if x1==0 and y1==0 and x2==0 and y2==0:
lbl3.config(text="ERROR select image area", fg="red")
running = False
frequency = 2
else:
if int(frequency) > 0:
image = ImageGrab.grab(bbox=(x1, y1, x2, y2))
image.save(str(path)) #Can be changed to png or jpg
else:
lbl3.config(text="ERROR frequency must be at least 1 second and an integer", fg="red")
running = False
frequency = 2
else:
frequency = 2
root.after((1000*int(frequency)), scanning)
def start():
global running, frequency, path
frequency = txt1.get()
path = txt2.get()
lbl3.config(text="Running...",fg="green")
running = True
def stop():
global running
running = False
lbl3.config(text="Stopped",fg="red")
def snipping():
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
self.setGeometry(0, 0, screen_width, screen_height)
self.setWindowTitle(' ')
self.begin = QtCore.QPoint()
self.end = QtCore.QPoint()
self.setWindowOpacity(0.3)
QtWidgets.QApplication.setOverrideCursor(
QtGui.QCursor(QtCore.Qt.CrossCursor)
)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
print('Capture the screen...')
self.show()
def paintEvent(self, event):
qp = QtGui.QPainter(self)
qp.setPen(QtGui.QPen(QtGui.QColor('black'), 1))
qp.setBrush(QtGui.QColor(128, 128, 255, 128))
qp.drawRect(QtCore.QRect(self.begin, self.end))
def mousePressEvent(self, event):
self.begin = event.pos()
self.end = self.begin
self.update()
def mouseMoveEvent(self, event):
self.end = event.pos()
self.update()
def mouseReleaseEvent(self, event):
self.close()
global x1, y1, x2, y2
x1 = min(self.begin.x(), self.end.x())
y1 = min(self.begin.y(), self.end.y())
x2 = max(self.begin.x(), self.end.x())
y2 = max(self.begin.y(), self.end.y())
img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
lbl4.config(text="Area Selected ", fg="green")
cv2.imshow('Captured Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWidget()
window.show()
app.aboutToQuit.connect(app.deleteLater)
app.exec_()
#GUI--------------------------------------------------------------------------
root = Tk()
root.title("Screen Capture")
root.geometry('600x120')
#Labels
lbl1 = Label(root, text="Frequency (seconds)")
lbl1.grid(column=0, row=0)
lbl2 = Label(root, text=" File Path")
lbl2.grid(column=3, row=0)
lbl3 = Label(root, text="")
lbl3.grid(column=4, row=4)
lbl4 = Label(root, text="Select Image Area ")
lbl4.grid(column=0, row=2)
spacer = Label(root, text="")
spacer.grid(column=0, row=3)
spacer2 = Label(root, text="")
spacer2.grid(column=0, row=1)
#Txt Entries
txt1 = Entry(root, width=13)
txt1.grid(column=1, row=0)
txt1.insert(0,"60")
txt2 = Entry(root, width=50)
txt2.grid(column=4, row=0)
txt2.insert(0,"screen.png")
#Buttons
btn_start = Button(root, text="Start", command=start)
btn_start.grid(column=0, row=4)
btn_stop = Button(root, text="Stop", command=stop)
btn_stop.grid(column=1, row=4)
btn_snipping = Button(root, text="Snipping tool", command=snipping)
btn_snipping.grid(column=1, row=2)
root.after(1000, scanning)
root.mainloop()
这是我的第一篇文章,对于可能遗漏的任何内容,我深表歉意。我希望最后有一个.exe文件,最后我可以将其发送到另一台没有python的计算机上,但是我似乎无法使其正常工作。预先谢谢你。
更新
这是我在命令行中运行.exe文件时收到的错误消息,这是我收到的消息:
C:\Users\trepayne\Downloads\ScreenCapture\Dist\OriginalScript>OriginalScript.exe
Traceback (most recent call last):
File "OriginalScript.py", line 3, in <module>
File "c:\users\trepayne\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\PyQt5\__init__.py", line 41, in <module>
File "site-packages\PyQt5\__init__.py", line 33, in find_qt
ImportError: unable to find Qt5Core.dll on PATH
[13324] Failed to execute script OriginalScript