tags:

views:

1028

answers:

1

Hi all,

Im writing a PyQt app that takes some input in one widget, and then processes some text files.

What ive got at the moment is when the user clicks the "process" button a seperate window with a QTextEdit in it pops up, and ouputs some logging messages.

On Mac OS X this window is refreshed automatically and you cna see the process.

On Windows, the window reports (Not Responding) and then once all the proccessing is done, the log output is shown. Im assuming I need to refresh the window after each write into the log, and ive had a look around at using a timer. etc, but havnt had much luck in getting it working.

Below is the source code. It has two files, GUI.py which does all the GUI stuff and MOVtoMXF that does all the processing.

GUI.py

import os
import sys
import MOVtoMXF
from PyQt4.QtCore import *
from PyQt4.QtGui import *


 class Form(QDialog):

     def process(self):
           path = str(self.pathBox.displayText())
           if(path == ''):
                QMessageBox.warning(self, "Empty Path", "You didnt fill something out.")
                return
           xmlFile = str(self.xmlFileBox.displayText())
           if(xmlFile == ''):
                QMessageBox.warning(self, "No XML file", "You didnt fill something.")
                return
           outFileName = str(self.outfileNameBox.displayText())
           if(outFileName == ''):
                QMessageBox.warning(self, "No Output File", "You didnt do something")
                return
           print path + "  " + xmlFile + " " + outFileName
           mov1 = MOVtoMXF.MOVtoMXF(path, xmlFile, outFileName, self.log)
           self.log.show()
           rc = mov1.ScanFile()
           if( rc < 0):
               print "something happened"
           #self.done(0)


      def __init__(self, parent=None):
            super(Form, self).__init__(parent)
            self.log = Log()
            self.pathLabel = QLabel("P2 Path:")
            self.pathBox = QLineEdit("")
            self.pathBrowseB = QPushButton("Browse")
            self.pathLayout = QHBoxLayout()
            self.pathLayout.addStretch()
            self.pathLayout.addWidget(self.pathLabel)
            self.pathLayout.addWidget(self.pathBox)
            self.pathLayout.addWidget(self.pathBrowseB)

            self.xmlLabel = QLabel("FCP XML File:")
            self.xmlFileBox = QLineEdit("")
            self.xmlFileBrowseB = QPushButton("Browse")
            self.xmlLayout = QHBoxLayout()
            self.xmlLayout.addStretch()
            self.xmlLayout.addWidget(self.xmlLabel)
            self.xmlLayout.addWidget(self.xmlFileBox)
            self.xmlLayout.addWidget(self.xmlFileBrowseB)


            self.outFileLabel = QLabel("Save to:")
            self.outfileNameBox = QLineEdit("")
            self.outputFileBrowseB = QPushButton("Browse")
            self.outputLayout = QHBoxLayout()
            self.outputLayout.addStretch()
            self.outputLayout.addWidget(self.outFileLabel)
            self.outputLayout.addWidget(self.outfileNameBox)
            self.outputLayout.addWidget(self.outputFileBrowseB)

            self.exitButton = QPushButton("Exit")
            self.processButton = QPushButton("Process")
            self.buttonLayout = QHBoxLayout()
            #self.buttonLayout.addStretch()
            self.buttonLayout.addWidget(self.exitButton)
            self.buttonLayout.addWidget(self.processButton) 
            self.layout = QVBoxLayout()
            self.layout.addLayout(self.pathLayout)
            self.layout.addLayout(self.xmlLayout)
            self.layout.addLayout(self.outputLayout)
            self.layout.addLayout(self.buttonLayout)
            self.setLayout(self.layout)
            self.pathBox.setFocus()
            self.setWindowTitle("MOVtoMXF")

            self.connect(self.processButton, SIGNAL("clicked()"), self.process)
            self.connect(self.exitButton, SIGNAL("clicked()"), self, SLOT("reject()"))
            self.ConnectButtons()


class Log(QTextEdit):

    def __init__(self, parent=None):
        super(Log, self).__init__(parent)
        self.timer = QTimer()
        self.connect(self.timer, SIGNAL("timeout()"), self.updateText())
        self.timer.start(2000) 

    def updateText(self):
        print "update Called"

AND MOVtoMXF.py

import os
import sys
import time
import string
import FileUtils
import shutil
import re

    class MOVtoMXF:
    #Class to do the MOVtoMXF stuff.

    def __init__(self, path, xmlFile, outputFile, edit):
        self.MXFdict = {}
        self.MOVDict = {}
        self.path = path
        self.xmlFile = xmlFile
        self.outputFile = outputFile
        self.outputDirectory = outputFile.rsplit('/',1)
        self.outputDirectory = self.outputDirectory[0]
        sys.stdout = OutLog( edit, sys.stdout)



class OutLog():

    def __init__(self, edit, out=None, color=None):
        """(edit, out=None, color=None) -> can write stdout, stderr to a
        QTextEdit.
        edit = QTextEdit
        out = alternate stream ( can be the original sys.stdout )
        color = alternate color (i.e. color stderr a different color)
        """
        self.edit = edit
        self.out = None
        self.color = color



    def write(self, m):
        if self.color:
            tc = self.edit.textColor()
            self.edit.setTextColor(self.color)

        #self.edit.moveCursor(QtGui.QTextCursor.End)
        self.edit.insertPlainText( m )

        if self.color:
            self.edit.setTextColor(tc)

        if self.out:
            self.out.write(m)
        self.edit.show()

If any other code is needed (i think this is all that is needed) then just let me know.

Any Help would be great.

Mark

A: 

It looks like your are running an external program, capturing its output into a QTextEdit. I didn't see the code of Form.process, but I am guessing on windows your function waits for the external program to finish, then quickly dumps everything to the QTextEdit.

If your interface really is waiting for the other process to finish, then it will hang in the manner you describe. You'll need to look at subprocess or perhaps even popen to get the program's output in a "non-blocking" manner.

The key to avoiding "(Not Responding)" is to call QApplication.processEvents a few times every few seconds. The QTimer is not going to help in this case, because if Qt cannot process its events, it cannot call any signal handlers.

Peter Shinners
I've added some more of the code so you can look at the process event. As far as I can tell it's just initializing a class.
Mark Underwood
So it looks like you are actually capturing `sys.stdout` while you run a big Python function. The easiest thing to try is call `qg.QApplication.processEvents()` at the end of your `OutLog.write` method. Then the interface will get a chance to update anytime something prints to stdout.
Peter Shinners