Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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 PyQt4&flask:无法为位于不同线程中的父线程创建子线程_Python_Qt_Pyqt_Flask_Pyqt4 - Fatal编程技术网

Python PyQt4&flask:无法为位于不同线程中的父线程创建子线程

Python PyQt4&flask:无法为位于不同线程中的父线程创建子线程,python,qt,pyqt,flask,pyqt4,Python,Qt,Pyqt,Flask,Pyqt4,我正在尝试将http请求上的页面图像保存到flask服务器 这是我在运行这个东西时得到的信息 QObject:无法为位于不同线程中的父线程创建子线程 这是我的密码 import sys import time from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import * import Image from flask import Flask, Response, jsonify,request

我正在尝试将http请求上的页面图像保存到flask服务器

这是我在运行这个东西时得到的信息 QObject:无法为位于不同线程中的父线程创建子线程

这是我的密码

import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import Image
from flask import Flask, Response, jsonify,request
app=Flask(__name__)


class Screenshot(QWebView):
    def __init__(self):
        self.app = QApplication(sys.argv)
        QWebView.__init__(self)
        self._loaded = False
        self.loadFinished.connect(self._loadFinished)

    def capture(self,url,width,output_file):
        self.resize(width,300)
        self.load(QUrl(url))
        self.wait_load()
        # set to webpage size
        frame = self.page().mainFrame()
        self.page().setViewportSize(frame.contentsSize())
        # render image
        image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
        painter = QPainter(image)
        frame.render(painter)
        painter.end()
        print 'saving', output_file
        image.save(output_file)

    def wait_load(self, delay=0):
        # process app events until page loaded
        while not self._loaded:
            self.app.processEvents()
            time.sleep(delay)
        self._loaded = False

    def _loadFinished(self, result):
        self._loaded = True





if __name__=='__main__':
    s = Screenshot()

    @app.route('/image', methods=['GET','POST'])
    def getPicture():

        #reading args
        url=request.args.get('url')
        screenWidth=int(request.args.get('sw'))
        top=int(request.args.get('top'))
        left=int(request.args.get('left'))
        width=int(request.args.get('width'))
        height=int(request.args.get('height'))

        #cropping image
        s.capture(url,screenWidth,"temp.png")
        img=Image.open("temp.png")
        box=(left, top, left+width, top+height)
        area=img.crop(box)
        area.save("output","png")
        return "output.png"

        @app.route('/')
        def api_root():
            return 'Welcome'

    app.run(host='0.0.0.0',port=3000,debug=True)
每当我用手指打这个

curl http://0.0.0.0:3000/image?url=googlecom&sw=1920&top=100&left=100&width=200&height=200
我收到以下错误消息

QObject: Cannot create children for a parent that is in a different thread.
(Parent is Screenshot(0x7f9dbf121b90), parent's thread is QThread(0x7f9dbdb92240), current thread is QThread(0x7f9dbf11ed50)
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread

我不确定这是什么原因,但我没有看到self.app.exec调用的屏幕截图。如果没有它,QApplication实例将永远不会进入主循环。顺便说一句,我会在屏幕截图之外,但在你的“主要”部分实例化QApplication。不确定在这种特殊情况下它是否重要,但它可能会