gpt4 book ai didi

python - 向 PyQt5 QGroupBox 中的按钮添加功能

转载 作者:行者123 更新时间:2023-12-01 01:10:20 26 4
gpt4 key购买 nike

所以我想向 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

最佳答案

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

关于python - 向 PyQt5 QGroupBox 中的按钮添加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54920372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com