Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何从.exe(PyQt4 Python2.7 py2exe)保存.jpg文件?_Python 2.7_Pyqt4_Screenshot - Fatal编程技术网

Python 2.7 如何从.exe(PyQt4 Python2.7 py2exe)保存.jpg文件?

Python 2.7 如何从.exe(PyQt4 Python2.7 py2exe)保存.jpg文件?,python-2.7,pyqt4,screenshot,Python 2.7,Pyqt4,Screenshot,我的程序有问题,我正在使用pyqt4和python2.7,我使用py2exe创建.exe 问题是,它所创建的程序的屏幕截图没有保存在目录或其他任何方式中 这是setup.py from distutils.core import setup import py2exe setup(windows=['capture.py'], options={"py2exe": {"includes": ["sip", "PyQt4.QtGui", "PyQt4.QtCore"]}}) 这是capture

我的程序有问题,我正在使用pyqt4和python2.7,我使用py2exe创建.exe

问题是,它所创建的程序的屏幕截图没有保存在目录或其他任何方式中

这是setup.py

from distutils.core import setup
import py2exe

setup(windows=['capture.py'], options={"py2exe": {"includes": ["sip", "PyQt4.QtGui", "PyQt4.QtCore"]}})
这是capture.py

import os
import datetime
import time
import sys
from PyQt4 import QtCore
from PyQt4.QtGui import *


class CaptureScreenShoot (QtCore.QThread):

    def __init__(self):
        QtCore.QThread.__init__(self, parent=app)
        self.count_time = 0
        self.complete_path = ""
        self.signal = QtCore.SIGNAL("signal")
        self.signal_cap = QtCore.SIGNAL("signal")

    def run(self):
        while True:
            now = datetime.datetime.now()
            date = str(now.strftime("%Y_%m_%d_%H_%M_%S"))
            currentPath = QtCore.QDir.homePath() + os.sep + "perfq_id_29" + os.sep
            dire = QtCore.QDir()
            if not dire.exists(currentPath):
                dire.mkpath(currentPath)
            filename = "Perfq29_" + date + ".jpg"
            self.complete_path = currentPath + filename
            self.emit(self.signal_cap, self.complete_path)
            #p = QPixmap.grabWindow(QApplication.desktop().winId())
            #print p.save(complete_path, 'jpg')
            self.count_time = self.count_time + 1
            self.emit(self.signal, self.count_time)
            time.sleep(10)


class CaptureTest(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self.setWindowTitle("Modulo de Captura de pantalla")
        self.move(500, 250)

        vBox = QVBoxLayout(self)

        self.boton = QPushButton("Start the Thread", self)
        self.label = QLabel("")
        self.label.hide()

        vBox.addWidget(self.boton)
        vBox.addWidget(self.label)

        self.boton.clicked.connect(self.call)

    def call(self):
        cap = CaptureScreenShoot()
        self.connect(cap, cap.signal_cap, self.picture)
        self.connect(cap, cap.signal, self.timer)
        cap.start()

    def timer(self, count):
        self.label.setText("Se han tomado <b> %s </b> capturas" % str(count))
        self.label.show()

    def picture(self, path):
        p = QPixmap.grabWindow(QApplication.desktop().winId())
        p.save(str(path), 'jpg')

app = QApplication(sys.argv)
w = CaptureTest()
w.show()
sys.exit(app.exec_())
导入操作系统
导入日期时间
导入时间
导入系统
从PyQt4导入QtCore
从PyQt4.QtGui导入*
类捕获截图(QtCore.QThread):
定义初始化(自):
QtCore.QThread.\uuuuu init\uuuuu(self,parent=app)
self.count\u time=0
self.complete_path=“”
self.signal=QtCore.signal(“信号”)
self.signal_cap=QtCore.signal(“信号”)
def运行(自):
尽管如此:
now=datetime.datetime.now()
date=str(现在是.strftime(“%Y\u%m\u%d\u%H\u%m\u%S”))
currentPath=QtCore.QDir.homePath()+os.sep+“perfq_id_29”+os.sep
dire=QtCore.QDir()
如果不存在(当前路径):
dire.mkpath(currentPath)
filename=“Perfq29”+日期+“.jpg”
self.complete\u path=当前路径+文件名
self.emit(self.signal\u cap、self.complete\u路径)
#p=QPixmap.grabWindow(QApplication.desktop().winId())
#打印p.save(完成路径“jpg”)
self.count\u time=self.count\u time+1
自我发射(自我信号、自我计数时间)
时间。睡眠(10)
类CaptureTest(QWidget):
定义初始化(自):
QWidget.\uuuuu初始化(自)
self.setWindowTitle(“Captura de pantalla模数”)
自行移动(500250)
vBox=QVbox布局(自)
self.boton=QPushButton(“启动线程”,self)
self.label=QLabel(“”)
self.label.hide()
vBox.addWidget(self.boton)
vBox.addWidget(self.label)
self.boton.clicked.connect(self.call)
def呼叫(自我):
cap=捕获截图()
自我连接(cap,cap.signal\U cap,自我图片)
自动连接(上限、上限信号、自动定时器)
第一章开始()
def定时器(自身、计数):
self.label.setText(“Se han tomado%s capturas”%str(count))
self.label.show()
def图片(自身,路径):
p=QPixmap.grabWindow(QApplication.desktop().winId())
p、 保存(str(路径),'jpg')
app=QApplication(sys.argv)
w=捕获测试()
w、 show()
sys.exit(app.exec_())

我认为问题在于,您的exe在运行时找不到
imageformats
插件目录。解释器中的PyQt可以,但exe不能

最简单的解决方案是将目录从PyQt(类似于
Python27\Lib\site packages\PyQt4\plugins\imageformats
)复制到与可执行文件位于同一目录中的某个位置。确保它在那里被称为
imageformats

不确定确切的目录-我使用PySide,但我认为它非常相似

我使用setup.py进行复制

ifpath = os.path.join(sys.prefix,"Lib","site-packages","PySide","plugins","imageformats")

distutils.dir_util.copy_tree(ifpath,dist_dir)

你是说程序在解释器中运行正常,但一旦使用py2exe就停止工作了吗?是的,事实上,当我使用png时,问题已经解决了,这是否意味着我不能在.exe中使用jpg?没有任何在解释器中工作的东西也可以在exe中工作。但是您需要正确地构建它。我们需要更多地了解它是如何失败的。你收到错误信息了吗?执行到什么程度?很有可能save调用抛出异常,但pyqt会捕获并丢弃该异常。尝试在Try except中包装要保存的调用,并打印收到的任何异常。程序没有停止,但当我运行.exe时,save函数返回false,但在解释器中它工作正常。