python - 如何将变量从 PyQt5 UI 返回到 Main 函数 - Python

标签 python pyqt pyqt5 qdialog

我使用pyqt5设计了一个自定义的formlayout ui,并希望将变量导入回主函数以进一步执行主函数。

当单击“确定”按钮时,我尝试了多种方法从主函数获取返回值,但无法从主函数获取变量。

你能指导我吗,我如何从 pyqt5 formlayout ui 获取变量到主函数 -

这是 PyQt5 FormLayout UI 函数的代码 -

from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog,
        QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout,
        QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit,
        QVBoxLayout,QCheckBox)

import sys

app = QApplication([])

class Dialog(QDialog):  

    def __init__(self,dinput):
        super(Dialog, self).__init__()
        self.createFormGroupBox(dinput)        

        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)

        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.formGroupBox)        
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)        

        self.setWindowTitle("Form Layout")    


    def accept(self):        
        print(self.linedit1.text())
        print(self.combox1.currentText())        
        print(self.spinbox1.value())       
        self.closeEvent()

    def reject(self):
        print('Cancelled')
        self.closeEvent()

    def getoutput(self):
        return self.linedit1.text()

    def createFormGroupBox(self,dinput):
        self.formGroupBox = QGroupBox("Form layout")
        layout = QFormLayout()

        self.linedit1 = QLineEdit()        
        self.linedit1.setText('TestName')
        layout.addRow(QLabel(dinput[0]), self.linedit1)        
        self.combox1 = QComboBox()
        self.combox1.setToolTip('Hello')
        self.combox1.addItems(['India','France','UK','USA','Germany'])
        layout.addRow(QLabel(dinput[1]), self.combox1)        
        self.spinbox1 = QSpinBox()        
        layout.addRow(QLabel(dinput[2]), self.spinbox1)        
        self.formGroupBox.setLayout(layout)

主要功能是 -

import os
import sys
import pyformlayout as pyfl

# Staring Functions for Execution

dinput = ['LastName','Country','Age']
# Call the UI and get the inputs
dialog = pyfl.Dialog(dinput)
if(dialog.exec_()):
    TName = dialog.getoutput
    print('------------------')
    print(TName)

# Main Function Continous by getting the inputs
# from UI

我无法获得输出函数所需的值。即使我已经使用 getoutput 函数返回值并将输出获取到“TName”。但我无法将值放入 TName 变量中,并且没有显示任何内容。

我得到的结果是 - (这基本上是打印接受按钮函数,而不是返回到 Main 函数的 TName 变量。

TestName
India
25

如何获取从 PyQt5 Formlayout UI 函数到 Main 函数的返回值..?

最佳答案

首先,FormLayout 是一个布局,即负责在窗口内定位小部件的类,因此它与这些情况无关。另一方面,永远不应该调用 closeEvent(),它是一个用于处理关闭窗口事件的函数。

当按下 Ok 时,accept 方法就会被调用,因此它是获取值的正确位置,因此必须将其存储在变量中,然后在 get_output() 中返回> 方法:

pyformlayout.py

import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

class Dialog(QtWidgets.QDialog):  
    def __init__(self, dinput):
        super(Dialog, self).__init__()
        self.createFormGroupBox(dinput)        

        buttonBox = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)

        mainLayout = QtWidgets.QVBoxLayout(self)
        mainLayout.addWidget(self.formGroupBox)        
        mainLayout.addWidget(buttonBox)     

        self.setWindowTitle("Form Layout")    

    def createFormGroupBox(self, dinput):
        layout = QtWidgets.QFormLayout()
        self.linedit1 = QtWidgets.QLineEdit('TestName')
        self.combox1 = QtWidgets.QComboBox()
        self.combox1.setToolTip('Hello')
        self.combox1.addItems(['India','France','UK','USA','Germany'])
        self.spinbox1 = QtWidgets.QSpinBox()  

        for text, w in zip(dinput, (self.linedit1, self.combox1, self.spinbox1)):
            layout.addRow(text, w)     

        self.formGroupBox = QtWidgets.QGroupBox("Form layout")        
        self.formGroupBox.setLayout(layout)

    def accept(self):        
        self._output = self.linedit1.text(), self.combox1.currentText(), self.spinbox1.value()    
        super(Dialog, self).accept()

    def get_output(self):
        return self._output

在文件 main.py 中,如果仅按下“确定”按钮,我就会得到该值:

ma​​in.py

import pyformlayout as pyfl

# Staring Functions for Execution
dinput = ['LastName','Country','Age']
# Call the UI and get the inputs
dialog = pyfl.Dialog(dinput)
if dialog.exec_() == pyfl.Dialog.Accepted:
    name, item, value = dialog.get_output()
    print(name, item, value)

关于python - 如何将变量从 PyQt5 UI 返回到 Main 函数 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52592977/

相关文章:

python - scipy.weave.inline 无法按预期与数学库一起工作

multithreading - 根据导入包(PyQt5)中打印的标准输出更新 PyQt 进度条

python - matplotlib yticks 中的值错误

python - 如何在 keras 神经网络中进行简单的数据记忆

python - 您将如何提高该功能的性能?

python - 在 PyQt 中自定义 MPL

python - 使用 QWebEngineView 显示大于 2MB 的内容?

qt - 设置 QFormLayout QLabel 的垂直对齐方式

python - 没有来自 QProcess 的 readyReadStandardOutput 信号

python - 在 QQuickWidget 内更新/重新绘制仪表