使用 QPushbutton 将变量传递到第二个脚本并执行它
Posted
技术标签:
【中文标题】使用 QPushbutton 将变量传递到第二个脚本并执行它【英文标题】:Using QPushbutton to pass variables into a second script and execute it 【发布时间】:2021-08-25 19:02:14 【问题描述】:我正在尝试使用 PyQt5 gui 的输入值来运行第二个 python 脚本。 我正在猜测的问题是:无法从“GeneratorApp”导入名称“outer_radius”(E:\Blank generator\GeneratorApp.py)
我假设这是因为变量不是全局变量,我发现我可以使用“Qprocess()”,但我不知道这可能如何工作,因为我是一个完全的编程初学者。 我的主要代码是:
from PyQt5 import QtCore, QtGui, QtWidgets
import subprocessCaller
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#Main window setup
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 371)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
#Generate button setup
self.GenerateBlanks = QtWidgets.QPushButton(self.centralwidget)
self.GenerateBlanks.setObjectName("GenerateBlanks")
self.gridLayout_2.addWidget(self.GenerateBlanks, 12, 0, 1, 1)
self.GenerateBlanks.clicked.connect(self.generate)
#Qline edit setup
self.MinUcSize = QtWidgets.QLineEdit(self.centralwidget)
self.MinUcSize.setObjectName("MinUcSize")
self.gridLayout_2.addWidget(self.MinUcSize, 7, 1, 1, 1)
self.MaterialDensity = QtWidgets.QLineEdit(self.centralwidget)
self.MaterialDensity.setObjectName("MaterialDensity")
self.gridLayout_2.addWidget(self.MaterialDensity, 6, 1, 1, 1)
self.BoreRadius = QtWidgets.QLineEdit(self.centralwidget)
self.BoreRadius.setObjectName("BoreRadius")
self.gridLayout_2.addWidget(self.BoreRadius, 4, 1, 1, 1)
self.FileDirectory = QtWidgets.QLineEdit(self.centralwidget)
self.FileDirectory.setObjectName("FileDirectory")
self.gridLayout_2.addWidget(self.FileDirectory, 1, 1, 1, 1)
self.OuterRadius = QtWidgets.QLineEdit(self.centralwidget)
self.OuterRadius.setObjectName("OuterRadius")
self.gridLayout_2.addWidget(self.OuterRadius, 2, 1, 1, 1)
self.MaxUcSize = QtWidgets.QLineEdit(self.centralwidget)
self.MaxUcSize.setObjectName("MaxUcSize")
self.gridLayout_2.addWidget(self.MaxUcSize, 8, 1, 1, 1)
self.BlankWidth = QtWidgets.QLineEdit(self.centralwidget)
self.BlankWidth.setObjectName("BlankWidth")
self.gridLayout_2.addWidget(self.BlankWidth, 5, 1, 1, 1)
self.InnerRadius = QtWidgets.QLineEdit(self.centralwidget)
self.InnerRadius.setObjectName("InnerRadius")
self.gridLayout_2.addWidget(self.InnerRadius, 3, 1, 1, 1)
self.NumberofGenerations = QtWidgets.QLineEdit(self.centralwidget)
self.NumberofGenerations.setObjectName("NumberofGenerations")
self.gridLayout_2.addWidget(self.NumberofGenerations, 11, 1, 1, 1)
#Horizental sliders setup
self.RowSpacingFactor = QtWidgets.QSlider(self.centralwidget)
self.RowSpacingFactor.setMinimum(1)
self.RowSpacingFactor.setMaximum(10)
self.RowSpacingFactor.setOrientation(QtCore.Qt.Horizontal)
self.RowSpacingFactor.setObjectName("RowSpacingFactor")
self.gridLayout_2.addWidget(self.RowSpacingFactor, 9, 1, 1, 1)
self.ElementSpacingFactor = QtWidgets.QSlider(self.centralwidget)
self.ElementSpacingFactor.setMinimum(1)
self.ElementSpacingFactor.setMaximum(10)
self.ElementSpacingFactor.setOrientation(QtCore.Qt.Horizontal)
#Labels setup
self.fileDirectory = QtWidgets.QLabel(self.centralwidget)
self.fileDirectory.setObjectName("fileDirectory")
self.gridLayout_2.addWidget(self.fileDirectory, 1, 0, 1, 1)
self.blankWidth = QtWidgets.QLabel(self.centralwidget)
self.blankWidth.setObjectName("blankWidth")
self.gridLayout_2.addWidget(self.blankWidth, 5, 0, 1, 1)
self.boreRadius = QtWidgets.QLabel(self.centralwidget)
self.boreRadius.setObjectName("boreRadius")
self.gridLayout_2.addWidget(self.boreRadius, 4, 0, 1, 1)
self.blankOuterRadius = QtWidgets.QLabel(self.centralwidget)
self.blankOuterRadius.setObjectName("blankOuterRadius")
self.gridLayout_2.addWidget(self.blankOuterRadius, 2, 0, 1, 1)
self.maximumUnitCellSize = QtWidgets.QLabel(self.centralwidget)
self.maximumUnitCellSize.setObjectName("maximumUnitCellSize")
self.gridLayout_2.addWidget(self.maximumUnitCellSize, 8, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout_2.addWidget(self.label, 10, 0, 1, 1)
self.rowSpacingFactor = QtWidgets.QLabel(self.centralwidget)
self.rowSpacingFactor.setObjectName("rowSpacingFactor")
self.gridLayout_2.addWidget(self.rowSpacingFactor, 9, 0, 1, 1)
self.blankInnerRadius = QtWidgets.QLabel(self.centralwidget)
self.blankInnerRadius.setObjectName("blankInnerRadius")
self.gridLayout_2.addWidget(self.blankInnerRadius, 3, 0, 1, 1)
self.minimumUnitCellSize = QtWidgets.QLabel(self.centralwidget)
self.minimumUnitCellSize.setObjectName("minimumUnitCellSize")
self.gridLayout_2.addWidget(self.minimumUnitCellSize, 7, 0, 1, 1)
self.ElementSpacingFactor.setObjectName("ElementSpacingFactor")
self.gridLayout_2.addWidget(self.ElementSpacingFactor, 10, 1, 1, 1)
self.materialDensity = QtWidgets.QLabel(self.centralwidget)
self.materialDensity.setObjectName("materialDensity")
self.gridLayout_2.addWidget(self.materialDensity, 6, 0, 1, 1)
self.numberofGenerations = QtWidgets.QLabel(self.centralwidget)
self.numberofGenerations.setObjectName("numberofGenerations")
self.gridLayout_2.addWidget(self.numberofGenerations, 11, 0, 1, 1)
self.patternControlParameters = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.patternControlParameters.setFont(font)
self.patternControlParameters.setObjectName("patternControlParameters")
self.gridLayout_2.addWidget(self.patternControlParameters, 0, 0, 1, 2)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.GenerateBlanks.setText(_translate("MainWindow", "Generate blanks"))
self.MinUcSize.setText(_translate("MainWindow", "2"))
self.MaterialDensity.setText(_translate("MainWindow", "7.86e-3"))
self.BoreRadius.setText(_translate("MainWindow", "15.00"))
self.FileDirectory.setText(_translate("MainWindow", "E:/FreeCAD macro/Gear generator"))
self.OuterRadius.setText(_translate("MainWindow", "50.00"))
self.fileDirectory.setText(_translate("MainWindow", "File directory:"))
self.blankWidth.setText(_translate("MainWindow", "Blank width:"))
self.BlankWidth.setText(_translate("MainWindow", "20.00"))
self.boreRadius.setText(_translate("MainWindow", "Bore radius:"))
self.MaxUcSize.setText(_translate("MainWindow", "10"))
self.blankOuterRadius.setText(_translate("MainWindow", "Blank outer radius:"))
self.InnerRadius.setText(_translate("MainWindow", "10.00"))
self.NumberofGenerations.setText(_translate("MainWindow", "1"))
self.maximumUnitCellSize.setText(_translate("MainWindow", "Maximum unit cell size:"))
self.label.setText(_translate("MainWindow", "Element spacing factor:"))
self.rowSpacingFactor.setText(_translate("MainWindow", "Row spacing factor:"))
self.blankInnerRadius.setText(_translate("MainWindow", "Blank inner radius:"))
self.minimumUnitCellSize.setText(_translate("MainWindow", "Minimum unit cell size:"))
self.materialDensity.setText(_translate("MainWindow", "Material density:"))
self.numberofGenerations.setText(_translate("MainWindow", "Number of generations:"))
self.patternControlParameters.setText(_translate("MainWindow", "Pattern control parameters:"))
def generate(self):
file_directory = str(self.FileDirectory.text())
outer_radius = float(self.OuterRadius.text())
inner_radius = float(self.InnerRadius.text())
bore_radius = float(self.BoreRadius.text())
blank_width = float(self.BlankWidth.text())
material_density = float(self.MaterialDensity.text())
min_uc_size = int(self.MinUcSize.text())
max_uc_size = int(self.MaxUcSize.text())
row_spacing_factor = int(self.RowSpacingFactor.value())/10
element_spacing_factor = int(self.ElementSpacingFactor.value())/10
iterations = int(self.NumberofGenerations.text())
## calling the file(path)
if __name__ == "__main__":
c = subprocessCaller.CallPy("E:\Blank generator\guest.py")
c.call_python_file()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我用来执行“guest.py”文件的“subProcessCaller”代码是:
from subprocess import call
class CallPy:
def __init__(self, path):
self.path = path
def call_python_file(self):
call(["Python", "". format(self.path)])
最后,我希望通过单击 QpushButto 执行的过程是从主过程导入的 2 个变量的简单组合(来自 gui 的输入):
from GeneratorApp import outer_radius, inner_radius
print(f"outer_radius + inner_radius")
【问题讨论】:
【参考方案1】:您收到导入错误,因为 outer_radius 超出范围。 outer_radius 的声明方式,它是一个局部变量,只在generate
方法中有效。
有些人主张将程序逻辑与 GUI 分开。我不知道这是否是你在这里想要做的。我从来没有费心去做。
如果是我,我会做 external_radius 类级别的变量,例如
self.outer_radius = outer_radius = float(self.OuterRadius.text()) # instead of outer_radius = ...
在 init 方法中,添加您的按钮
self.mybutton = QPushButton('Sum', parent = self.centralwidget, objectName = 'mybutton')
self.mybutton.clicked.connect(on_mybutton_clicked)
然后为 Ui_MainWindow 新建一个方法
def on_mybutton_clicked(self):
print(self.outer_radius + self.inner_radius)
【讨论】:
以上是关于使用 QPushbutton 将变量传递到第二个脚本并执行它的主要内容,如果未能解决你的问题,请参考以下文章
Objective C - 将数据从第二个 VC 与 tableview 传递到第一个 VC
如何将选定的图像从 ImagePickerController 传递到第二个视图控制器?
如何通过单击 QPushButton 打印 QTableWidget