向PyQt5 QGroupBox中的按钮添加功能

时间:2019-02-28 07:19:07

标签: python python-3.x pyqt pyqt5

所以我想向PyQt5 QGroupBox添加一个按钮。在将参数保存到类变量中之前,该按钮将关闭窗口。我该怎么做? 我的代码:

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()

当前看起来像这样:

enter image description here

1 个答案:

答案 0 :(得分:2)

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_())