我是PyQt和编码GUI的新手。我尝试使用子进程命令自动化其他软件。我有按钮来运行命令。这个过程里面有很多循环,我想显示循环是否完成并且是否成功。 Everythings工作,但我的问题是当我按下按钮,第二个窗口打开后,过程完成。我尝试处理QtGui.Qapplication.processEvent()
,但我真的不知道放在哪里。我使用迭代循环简化了问题。
import sys, os, subprocess
from PyQt4 import QtGui, QtCore
import externalCommands as run
import QtObject as QO
import resource
resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))
# Don't write .pyc file
sys.dont_write_bytecode = True
class OtherWindow(QtGui.QMainWindow):
def __init__(self, working_folder):
super(OtherWindow, self).__init__()
self.setGeometry(150, 150, 640, 528)
self.setWindowTitle('Autre Fenetre ')
self.setWindowIcon(QtGui.QIcon('mc3icon.jpg'))
self.working_folder = working_folder
#---home display---
self.home()
def home(self):
QO.button('Abort', 530, 490, 'default', 'default', self.abort_calculation, self)
for tc in range(len(self.working_folder)):
currentWorkingFolder = os.path.abspath('../../workingfolder/' + self.working_folder[tc])
os.chdir(currentWorkingFolder)
QO.label('Running ' + str(self.working_folder[tc] ), 50, 0 + 50 * tc, self)
source = run.checked_paramter_files('ms*', '*.exe')
if (len(source) == 0):
QO.label('***ERROR*** File *dcp, *.dat, *.exe not found', 250, 0 + 50 * tc, self)
QO.picture("unchecked.png", 590, 12.5 + 50*tc, 30, 25, self)
else:
command = ('./program.exe descriptionFile.dcp')
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
for line in iter(p.stdout.readline, b''):
print line,
QO.picture("checked.png", 590, 12.5 + 50*tc, 25, 25, self)
def abort_calculation(self):
#---pop message box---
choice = QtGui.QMessageBox.question(self, 'Abort', 'Are you sure you to stop? \n Calculation will abort', QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if choice == QtGui.QMessageBox.Yes:
self.close()
else:
pass
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 1024, 768)
self.setWindowTitle('GUI test ')
self.setWindowIcon(QtGui.QIcon('image.jpg'))
image = QtGui.QLabel(self)
image.setGeometry(390, 25, 800, 260)
pixmap = QtGui.QPixmap('image.png')
image.setPixmap(pixmap)
image.show()
self.working_folder = os.listdir(os.path.abspath('../../workingfolder/.'))
#---bar menu---
extractAction = QtGui.QAction('&Quit', self)
extractAction.setShortcut("Ctrl+Q")
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
optionMenu = mainMenu.addMenu('&Options')
fileMenu.addAction(extractAction)
self.labelWarning = QtGui.QLabel(self)
self.labelWarning.setText('***WARNING***')
self.labelWarning.setStyleSheet('color: red')
self.labelWarning.move(420, 100)
self.labelWarning.resize(600, 600)
#---home display---
self.home()
def home(self):
#---button : quit ---
QO.button('Quit', 910, 710, 'default', 'default', self.close_application, self)
#---button : run fortran sofware ---
QO.button('Run ', 570, 450, 'default', 'default', self.open_new_window, self)
#---tree widget : list available folders to run fortran software---
self.treeWidget = QtGui.QTreeWidget(self)
self.treeWidget.setGeometry(QtCore.QRect(0, 75, 380, 650))
self.treeWidget.setHeaderLabel('Available Test Cases')
for i in xrange(len(self.working_folder)):
parent = QtGui.QTreeWidgetItem(self.treeWidget)
parent.setText(0, str(self.working_folder[i]))
parent.setFlags(parent.flags() | QtCore.Qt.ItemIsTristate | QtCore.Qt.ItemIsUserCheckable)
tmp_list_files = os.listdir(os.path.abspath('../../workingfolder/' + self.working_folder[i] + "/."))
for x in xrange(len(tmp_list_files)):
child = QtGui.QTreeWidgetItem(parent)
child.setFlags(child.flags() | QtCore.Qt.ItemIsUserCheckable)
child.setText(0, tmp_list_files[x])
child.setCheckState(0, QtCore.Qt.Unchecked)
#---show---
self.show()
def close_application(self):
#---pop message box---
choice = QtGui.QMessageBox.question(self, 'Quit', 'Are you sure you to quit?', QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if choice == QtGui.QMessageBox.Yes:
print('Program closed sucessfully')
sys.exit()
else:
pass
def open_new_window(self):
self.newWindow = OtherWindow(self.working_folder)
self.newWindow.show()
def main():
app = QtGui.QApplication (sys.argv)
GUI = Window()
sys.exit(app.exec_())
main()
感谢您的帮助。