添加相应的功能在PyQt5 QGroupBox按钮(Add functionalities to b

2019-09-28 01:21发布

所以我想一个按钮添加到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()

目前,它看起来像这样:

Answer 1:

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


文章来源: Add functionalities to buttons in PyQt5 QGroupBox