2017-09-05 48 views
1

我遇到了一個問題,試圖通過按鈕單擊來調用函數時,我的程序不會凍結。該函數使用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創建的代碼?

回答

0

我通過簡單地刪除process.wait()來解決問題。我仍然對如何使用QtDesigner創建的代碼使用線程尋找解決方案感興趣。