[英]PyQt5: Validate LineEdit through QPushButton using signals and slots
將QPushButton連接到PyQt5中的QMessageBox時遇到麻煩,因為與PyQt4相比似乎沒有多少文檔。 就目前情況而言,QmessageBox在主布局之前執行,我認為這是.self
和.exec_()
嗎?
但是,第二個也是主要的問題是有關連接兩個小部件的問題。 我正在尋求實施某種形式的有效性檢查; 即,當兩個QLineEdit
字段均包含文本時,則在單擊“提交”時,表單應清除該字段,但是,如果在單擊“提交”時將兩個字段中的任何一個都留為空白,則Id喜歡打開QMessageBox
。 我不確定如何實現此功能,因為我不知道如何將textField和PushButton連接在一起。
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()
開始,這意味着Buttons.__init__
方法將運行。 在這個運行Buttons.messageBox
連接上述messageBox
槽到clicked
的信號button1
。 然后,它將打開消息框( self.message.exec_()
)。 您的__init__
方法最終完成,然后調用app.exec_()
,它實際上顯示主GUI並啟動Qt事件循環,該循環處理諸如單擊按鈕和重繪事件之類的事情。
至此,您的程序已經被告知顯示消息框,並配置為在單擊按鈕時運行Buttons.messageBox
。 單擊該按鈕時,它將顯示消息框,並為該按鈕建立其他連接。 下次單擊按鈕時,將顯示2個消息框!
解決方案是從messageBox
插槽中分離出信號連接。 與其在__init__
方法中調用self.messageBox()
, 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_()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.