So I wanted to add a button to a PyQt5 QGroupBox. The button will close the window before saving the parameters in class variables. How would I do this?
My code:
def changeDefaultParameters(self):
self.DPGroupBox = QGroupBox("Default Paramters:")
self.DPGroupBox.setStyleSheet('QGroupBox {color: blue; font:bold 14px}')
self.DPGroupBox.move(300, 300)
layout = QFormLayout()
layout.addRow(QLabel("D (nm):"), QLineEdit())
layout.addRow(QLabel("R_on (\u03A9):"), QLineEdit())
layout.addRow(QLabel("R_off (\u03A9):"), QLineEdit())
layout.addRow(QLabel("W_0 (nm):"), QLineEdit())
layout.addRow(QLabel("Mobility (\u03BC):"), QLineEdit())
layout.addRow(QLabel("Polarity (\u03B7):"), QLineEdit())
layout.addRow(QLabel(), QPushButton('OK', self))
# ADD SOMETHING HERE SO THAT WHEN OK IS CLICKED THE
VALUES INPUT GETS SAVED SO CLASS VARIABLES LIKE:
self.var1 = D
ALSO THE WINDOW CLOSES
self.DPGroupBox.setLayout(layout)
self.DPGroupBox.show()
Currently it looks like this:
from PyQt5 import QtWidgets, QtCore
class Data(object):
def __init__(self, parent = None):
self.d = 0
self.r_on = 0
self.r_off = 0
self.w = 0
self.mobility = 0
self.polarity = 0
def toString(self):
print(self.d, self.r_on, self.r_off, self.w, self.mobility, self.polarity)
def set_d(self, value):
self.d = value
class Form(QtWidgets.QWidget):
def __init__(self, parent = None):
super(Form, self).__init__(parent)
self.DPGroupBox = QtWidgets.QGroupBox("Default Paramters:")
#self.DPGroupBox.setStyleSheet('QGroupBox {color: blue; font:bold 14px}')
self.DPGroupBox.move(300, 300)
layout = QtWidgets.QFormLayout()
self.txtD = QtWidgets.QLineEdit(self)
self.txtR_on = QtWidgets.QLineEdit(self)
self.txtR_off = QtWidgets.QLineEdit(self)
self.txtW = QtWidgets.QLineEdit(self)
self.txtMobility = QtWidgets.QLineEdit(self)
self.txtPolarity = QtWidgets.QLineEdit(self)
btnOk = QtWidgets.QPushButton('OK', self)
layout.addRow(QtWidgets.QLabel("D (nm):"), self.txtD)
layout.addRow(QtWidgets.QLabel("R_on (\u03A9):"), self.txtR_on)
layout.addRow(QtWidgets.QLabel("R_off (\u03A9):"), self.txtR_off)
layout.addRow(QtWidgets.QLabel("W_0 (nm):"), self.txtW)
layout.addRow(QtWidgets.QLabel("Mobility (\u03BC):"), self.txtMobility)
layout.addRow(QtWidgets.QLabel("Polarity (\u03B7):"), self.txtPolarity)
layout.addRow(QtWidgets.QLabel(), btnOk)
self.DPGroupBox.setLayout(layout)
self.DPGroupBox.show()
btnOk.clicked.connect(self.okClicked)
def okClicked(self):
dataA = Data()
dataA.d = self.txtD.text()
## or you can use a setter like so:
#dataA.set_d(self.txtD.text())
dataA.r_on = self.txtR_on.text()
dataA.r_off = self.txtR_off.text()
dataA.w = self.txtW.text()
dataA.mobility = self.txtMobility.text()
dataA.polarity = self.txtPolarity.text()
print(dataA.toString())
QtCore.QCoreApplication.instance().quit()
# or self.close() if it got an parent
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
myapp = Form()
sys.exit(app.exec_())