I have written a program that watches over a directory and alerts when a file is added by a user, the file has specific format as user-name.files it works fine but when I press ok of the alert received of new file being added, the program exits, I want it it to stay running.

The below code I have written will run as child process of another PYQT application within that application. So in that I won't be executing main() but will just instantiate SendMyFiles object.

from PyQt4 import QtGui,QtCore
from PyQt4.QtCore import pyqtSlot
import sys
import os

class SendMyfiles(QtGui.QMainWindow):
    def __init__(self):
        super(SendMyfiles, self).__init__()
        self._lookInPath = "/Users/krystosan"
        self.filesList = os.listdir(self._lookInPath)
        print self.filesList
        self.watchMyfilesBin()

    def watchMyfilesBin(self):
        self.fileSysWatcher = QtCore.QFileSystemWatcher()
        self.fileSysWatcher.addPath(self._lookInPath)
        QtCore.QObject.connect(self.fileSysWatcher,QtCore.SIGNAL("directoryChanged(QString)"), self,       
            QtCore.SLOT("slotDirChanged(QString)")) 
        # get list of files as files
        self.newFilesList = os.listdir(self._lookInPath)


    def _connections(self):
        pass

    def recievedfilesFromUser(self):
        newUsrFile =  list(set(os.listdir(self._lookInPath))^set(self.filesList))[0]
        userRecvdFrom = newUsrFile.split(".")[0]
        self.filesList.append(newUsrFile)
        return userRecvdFrom

    @pyqtSlot("QString")   
    def slotDirChanged(self, userfiles):
        userName = self.recievedfilesFromUser()
        retVal = QtGui.QMessageBox.about(self, "Hello %s" % os.getenv('USER'), "Recieved files from %s." % userName)


def main():
    app     = QtGui.QApplication(sys.argv)
    fileSysWatcher  = QtCore.QFileSystemWatcher() 
    window    = SendMyfiles()  
    app.exec_()

if __name__ == '__main__':
    main()
有帮助吗?

解决方案

Sorry, I cannot reproduce this one (OpenSUSE 12.3 x64, PyQt 4.9.6).

I took your code and added a line window.show() to main() (despite your comment saying that you were "doing window.show()"). I also replaced the line

    userRecvdFrom = newUsrFile(".")[0]

with

    userRecvdFrom = newUsrFile.split(".")[0]

The former gives a runtime error because newUsrFile is a string and you can't call it. I also changed the directory being watched as I don't have a directory with that name on my machine.

After doing this, I could reliably create files in the folder being watched and have the program pop up alert boxes. After each alert was dismissed the program stayed running.

So I can only speculate at what the problem might be. You say you are instantiating SendMyfiles, in code that you have chosen not to share with us, but how long does this object stay in existence for? Do you keep a reference to this object, or is it only stored in a local variable and hence gets garbage collected at the end of a method? If you have a PyQt window object, and all references to it are lost, Python will garbage-collect it, which will cause the underlying Qt C++ object to be deleted and the window closed.

其他提示

Well, you can find yopur answer at here, on a sidenote I guess you mean the terminal that Windows opens for you when you run a python file is closed too fast. You can add raw_input('Press Enter to exit') right before your program would exit. It tells Python to wait for input before exiting, please check here also.

Well I can't provide you a very satisfactory answer, but hopefully this will help.

Firstly, the code example you have provided does not call show() on the QMainWindow.

It then appears that creating a QMessageBox when no main window is shown causes the Qt event loop (started by app.exec_()) to stop once the QMessageBox is closed. So modifying your code as follows will cause you application to behave as you expect:

window    = SendMyfiles()  
window.show()
app.exec_()

However, what I can't explain is why the event loop started by app.exec_() would end when the event loop for the QMessageBox ends. If anyone has insight, I'd love to hear why this happens!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top