在PyQt5中將QPushButton連接到QMessageBox時遇到問題,因爲似乎與PyQt4相比幾乎沒有文檔。 QmessageBox在主佈局之前執行,我認爲這是一個與.self
和.exec_()
有關的問題?PyQt5:使用信號和插槽通過QPushButton驗證LineEdit
第二,然而主要問題是關於連接兩個小部件。我期待實施某種形式的有效性檢查;即當兩個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_())