线程化函数-PyQt GUI
问题描述:
我遇到了一个问题,试图通过按钮单击来调用函数时,我的程序不会冻结。该函数使用subprocess.Popen打开一个.bat文件,但在.bat运行时,它会冻结我的整个GUI,我希望用户能够继续使用GUI。我很新的线程。我很明白线程是这样一个解决方案,但我不确定如何让一个线程在点击按钮时调用这个函数。请记住我的功能在QtDesigner创建的窗口中。任何想法或方法我可以解决这个问题?也许把我带到某个能找到答案的地方?线程化函数-PyQt GUI
主类名:
类Ui_TestClass(对象):
我的功能:
def runprogram(self):
dir = self.cwdList[-1]
test = os.listdir(dir)
for item in test:
if item.endswith('.OUT'):
os.remove(join(dir, item))
new, ok = QInputDialog.getText(None, "Case Name","Type in a case name to run. (No Spaces) \nOn file dialog, choose file to rename.")
if (ok):
newcase = new
changename = str(QtWidgets.QFileDialog.getOpenFileName(None, 'Choose file', '{0}'.format(self.cwdList[-1]), 'Text files (*.ZCSP*)')[0])
shutil.move(changename, '{0}\{1}.ZCSP'.format(self.cwdList[-1], newcase))
self.replaceText('{0}.ZCSP'.format(newcase))
with open('Test.bat', "w") as runname:
run = 'ZCSP {0}.ZCSP {0}.OUT'.format(newcase, newcase)
runname.write(run)
process = subprocess.Popen([r"{0}\Test.bat".format(self.cwdList[-1])])
process.wait()
self.cwdList只是一个目录列表
t = threading.Thread(target=self.runprogram)
t.start()
我读过这可能会工作,但我将如何插入我的QtDesigner创建的代码?
答
我通过简单地删除process.wait()来解决问题。我仍然对如何使用QtDesigner创建的代码使用线程寻找解决方案感兴趣。