我想直接探讨问题的核心。我在类下,认为X
和Y
的值是用户输入,来自主窗口调用def fun
,这是在满足条件的情况下按预期工作,将Y = 3更改为看见。但是,如果不满足条件,则会出现错误窗口,但似乎并没有真正起作用,它会崩溃。 错误消息如何显示?
代码:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
class Mainwindow(QtWidgets.QWidget):
def __init__(self):
super(Mainwindow, self).__init__()
self.setMinimumSize(QtCore.QSize(300,400))
self.setWindowTitle('My Graphic Window')
self.AL = Cap.fun
Run_button = QtWidgets.QPushButton("Run")
Run_button.clicked.connect(self.AL)
button_layout = QtWidgets.QVBoxLayout(self,)
button_layout.addWidget(Run_button)
self.setLayout(button_layout)
class Cap:
def fun(self):
X = 3
Y = 4
W = 0
for i in range(0,21):
W = X*Y
if W == 3*X:
return print('Ok')
elif W > 5*Y:
return print('Not good result!')
print('Errors!!','Obs', sep=' ')
app = QtWidgets.QApplication([])
error_dialog = QtWidgets.QErrorMessage()
error_dialog.showMessage('Change values!!')
app.exec_()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
im = Mainwindow()
im.show()
sys.exit(app.exec_())