2015-07-10 20 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