2015-07-10 19 views
0

我遇到了幾個小問題PyQt4的工作的Python 2.7下而PyQt的self.close()()

我正在寫一個小項目,其中有一些QDialogs打開對方。 因此,有一個對話框打開,並立即打開另一個對話框來檢查某些內容,並且在出現錯誤檢查時,我想要關閉整個對話框。它看起來像這樣:

class MyDialog(QtGui.QDialog): 
    def __init__(self): 
     ## should get me a 10 digit number input 
     text, ok = QtGui.QInputDialog.getText(self, u'Enter check') 
    if ok: 
     ## if clicked ok, assign tID 
     tID = text 
    else: 
     ## else close 
     self.close() 

    try: 
     ## checker is a plausibilty check for the input 
     ## checks, if tID is a number and 10 digits long 
     ## and throws an IntegrityWarning if one of these conditions 
     ## is not met   
     tID = self.checker.checkID(tID) 
    except IntegrityWarning as e: 
     ## on exception show error dialog 
     errDialog = QtGui.QMessageBox() 
     errDialog.setWindowTitle(u'Fehler') 
     errDialog.setText(e.getWarning()) 
     ok = errDialog.exec_() 
     if ok != True: 
      ## close the whole dialog if user cancels 
      self.close() 

    self.tAccount = self.tControl.getAccount(tID) 

所以這基本上是我做的,它工作得很好,只是我得到分配之前,使用工貿署ERRORMESSAGE。此後我的程序運行良好,但我想知道錯誤來自哪裏。儘管我感到好奇,但事實是,即使我把tAccount的最後一個賦值放到try-except:pass語句中,錯誤也會被拋到控制檯,而不是通過,因爲它應該是顯而易見的。所以我的猜測是,我不能簡單地終止__init__()例程中的程序。我嘗試了一條return語句而不是close()語句,並且銷燬了MainWindow中的調用者函數中的QDialog。這使對話框出現並保持空白,因爲它是模態的,我必須關閉它才能在MainWindow中繼續。

有人能告訴我爲什麼Dialog __init__()在關閉後重新開始?

回答

1

這是Python的正常行爲。 self.close()不是返回聲明,因此它不會從__init__退出。

此外,__init__實際上沒有在屏幕上顯示對話框,所以在__init__中編寫self.close()毫無意義。我建議重構您的代碼,以便此應用程序邏輯位於__init__之外,並將根據QInputDialogcheckerchecker之間的結果決定是否初始化並顯示MyDialog