from PySide2 import QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.lineEdit = QtWidgets.QLineEdit()
self.lineEdit.setText("1")
self.lineEdit.editingFinished.connect(self.check)
self.lineEdit2 = QtWidgets.QLineEdit()
vlay = QtWidgets.QVBoxLayout(self)
vlay.addWidget(self.lineEdit)
vlay.addWidget(self.lineEdit2)
def check(self):
if self.lineEdit.text() == "1":
popup = QtWidgets.QMessageBox(self)
popup.setWindowTitle("why")
popup.show()
print("test")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
因此,在这个脚本中,如果在编辑“lineEdit”时按“Enter”,则“check”槽将被调用两次。但是,如果单击“lineEdit2”,插槽将只调用一次,这是应该的。这是因为QMessageBox,但是为什么呢
如果选中the docs:
(我的重点)
在您的情况下,第一次打印是在按Enter键时进行的,第二次打印是在QLineEdit自QMessageBox获取后失去焦点时进行的
如果要避免此行为,可以在QMessageBox显示前一刻阻止QLineEdit事件的发出,直到它显示后一刻:
相关问题 更多 >
编程相关推荐