0

I'm having problem with my program, I'm working with pyqt4 and python2.7 and I used py2exe to create the .exe

The problem it's that the screenshot that the programs it's supose to create are not being save in the directory or anyother way.

this it's the setup.py

from distutils.core import setup
import py2exe

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

and this it's 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_())
4
  • Are you saying that the program runs OK in the interpreter but stops working once you use py2exe?
    – strubbly
    Jul 13, 2015 at 16:58
  • Yes, actually the problem it's fixed when I use png, does that means I can't use jpg in .exe ??
    – SujaM
    Jul 14, 2015 at 16:45
  • No anything that works in the interpreter can also work in the exe. But you need to build it correctly. We need to find out more about how it is failing. Do you get an error message? How far does the execution get? It's quite possible that the save call is throwing an exception but this is being caught and discarded by pyqt. Try wrapping the call to save in a try except and printing any exception received.
    – strubbly
    Jul 15, 2015 at 7:42
  • The program doesn't stop, but the save function it's returning false when I run the .exe however in the interpreter it works perfectly.
    – SujaM
    Jul 17, 2015 at 18:21

1 Answer 1

0

I think the problem is that your exe cannot find the imageformats plugins directory at run time. PyQt in the interpreter can, but the exe cannot.

The simplest solution is just to copy the directory from PyQt (something like Python27\Lib\site-packages\PyQt4\plugins\imageformats) to somewhere in the same directory as your executable. Make sure it's called imageformats there.

Not sure of the precise directories - I use PySide, but I think it's very similar.

I get my setup.py to do the copying with

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

distutils.dir_util.copy_tree(ifpath,dist_dir)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.