我编写了一个程序来监视目录并在用户添加文件时发出警报,该文件的特定格式为 user-name.files 它工作正常,但是当我按 ok 收到新文件正在添加的警报时,程序退出,我希望它继续运行。
我编写的以下代码将作为该应用程序中另一个 PYQT 应用程序的子进程运行。因此,我不会执行 main(),而只会实例化 SendMyFiles 对象。
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()