我在 Python 2.7 下使用 PyQt4 时遇到了一些小问题
我正在编写一个小项目,其中有一些 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)
所以这基本上就是我所做的,它工作得很好,除了我在分配之前收到使用 tID 的错误消息。之后我的程序运行得很好,但我想知道错误来自哪里。但让我感到好奇的是,即使我将 tAccount 的最后一个赋值放入 try- except: pass 语句中,错误也会被抛出到控制台,而不是传递,因为它应该是明显的。所以我的猜测是我不能简单地终止程序__init__()
常规。我尝试使用 return 语句而不是 close() 语句,并销毁了 MainWindow 中调用函数中的 QDialog。这使得对话框出现并保持为空,因为它是模态的,我必须关闭它才能在主窗口中恢复。
有人可以告诉我为什么对话框__init__()
关闭后还能恢复吗?
这是 Python 的正常行为。self.close()
不是 return 语句,因此不会退出__init__
.
此外,__init__
实际上并没有在屏幕上显示对话框,所以写self.close()
in __init__
毫无意义。我建议重组您的代码,以便此应用程序逻辑超出__init__
并会决定是否初始化并显示MyDialog
根据结果QInputDialog
and checker
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)