Python PyQt5:使用信号和插槽通过QPushButton验证LineEdit
在PyQt5中将QPushButton连接到QMessageBox时遇到问题,因为与PyQt4相比,文档似乎很少。目前,QmessageBox在主布局之前执行,我认为这是.self和.exec的问题 然而,第二个也是主要的问题是关于连接两个小部件。我希望实施某种形式的有效性检查;i、 e如果两个QLineEdit字段都包含文本,则在单击“提交”时,表单应清除字段,但是如果在单击“提交”时其中一个字段为空,则需要打开QMessageBox。我不确定如何实现这一点,因为我不知道如何将文本字段和按钮连接在一起Python PyQt5:使用信号和插槽通过QPushButton验证LineEdit,python,qt,python-3.x,pyqt4,pyqt5,Python,Qt,Python 3.x,Pyqt4,Pyqt5,在PyQt5中将QPushButton连接到QMessageBox时遇到问题,因为与PyQt4相比,文档似乎很少。目前,QmessageBox在主布局之前执行,我认为这是.self和.exec的问题 然而,第二个也是主要的问题是关于连接两个小部件。我希望实施某种形式的有效性检查;i、 e如果两个QLineEdit字段都包含文本,则在单击“提交”时,表单应清除字段,但是如果在单击“提交”时其中一个字段为空,则需要打开QMessageBox。我不确定如何实现这一点,因为我不知道如何将文本字段和按钮连
from PyQt5.QtWidgets import QWidget, QLabel, QLineEdit,QSpinBox,
QDoubleSpinBox, QComboBox, QRadioButton, QPushButton, QHBoxLayout, QVBoxLayout,
QTextEdit, QGridLayout, QApplication, QMessageBox
from PyQt5.QtCore import Qt, pyqtSlot
import csv
class Buttons (QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.Widgets()
self.arrange()
self.close()
self.messageBox()
self.setWindowTitle ("test")
def Widgets(self):
self.nameLabel = QLabel("Name: ")
self.nameLineEdit = QLineEdit()
self.surnameLabel = QLabel("Surname: ")
self.surnameLineEdit = QLineEdit()
self.button1 = QPushButton("submit")
self.button2 = QPushButton("cancel")
self.button1.setMaximumSize(150,20)
self.button2.setMaximumSize(150,20)
def arrange (self):
nameField = QVBoxLayout()
nameField.addWidget(self.nameLabel)
nameField.addWidget(self.nameLineEdit)
nameField.addWidget(self.surnameLabel)
nameField.addWidget(self.surnameLineEdit)
#QHBoxLayout for Buttons:
buttonsLayout = QHBoxLayout()
buttonsLayout.addWidget(self.button1)
buttonsLayout.addWidget(self.button2)
self.button1.setSizePolicy(10,10)
self.button2.setSizePolicy(10,10)
#Creating mainLayout:
mainLayout = QVBoxLayout()
mainLayout.addLayout(nameField)
mainLayout.addLayout(buttonsLayout)
self.setLayout(mainLayout)
@pyqtSlot()
def close(self):
#close window
self.button2.clicked.connect(app.quit)
def clear(self):
pass
#Clear textFields when button is clicked:
@pyqtSlot()
def messageBox(self):
self.message = QMessageBox()
self.message.setText ("submit ERROR")
self.message.setStandardButtons(QMessageBox.Ok)
self.button1.clicked.connect(self.messageBox)
self.message.exec_()
我尝试过几种不同的技术,但都没有成功
self.connect(self.Button1, SIGNAL("clicked()"),self.clicked)
def clicked(self):
QMessageBox.about(self, "message!"(self.messageBox()))
if __name__ == "__main__":
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication (sys.argv)
window = Buttons()
window.show()
sys.exit (app.exec_())
仔细阅读您的程序并遵循执行路径。以window=Buttons开始,这意味着按钮。_uinit__;方法运行。您可以在其中运行Buttons.messageBox,它将messageBox插槽连接到button1的单击信号。然后,它打开消息框self.message.exec。您的_init__方法最终完成,然后调用app.exec,它实际显示主GUI并启动Qt事件循环,该循环处理按钮单击和重画事件等事件 此时,您的程序已被告知显示消息框,并配置为在单击按钮时运行Buttons.messageBox。单击按钮时,将显示消息框,并为按钮建立附加连接。下次单击该按钮时,将显示两个消息框 解决方案是将信号连接从messageBox插槽中分离出来。不要在_init__方法中调用self.messageBox,而是在那里建立button1的信号连接
class Buttons (QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.Widgets()
self.arrange()
self.close()
self.button1.clicked.connect(self.messageBox)
self.setWindowTitle ("test")
@pyqtSlot()
def messageBox(self):
self.message = QMessageBox()
self.message.setText ("submit ERROR")
self.message.setStandardButtons(QMessageBox.Ok)
self.message.exec_()