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

如何将变量从PyQt5 UI返回到主函数-Python,python,pyqt,pyqt5,qdialog,Python,Pyqt,Pyqt5,Qdialog,我已经使用pyqt5设计了一个定制的formlayout ui,并希望将变量导入主函数,以便进一步执行主函数 我尝试了很多方法,在单击“OK”按钮时从主函数获取返回值,但无法从主函数获取变量 TestName India 25 请您指导我,我如何从pyqt5 formlayout ui获取变量到主函数- from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog, QDialogButtonBox, QFormLa

我已经使用pyqt5设计了一个定制的formlayout ui,并希望将变量导入主函数,以便进一步执行主函数

我尝试了很多方法,在单击“OK”按钮时从主函数获取返回值,但无法从主函数获取变量

TestName
India
25
请您指导我,我如何从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)
以下是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变量中,并且没有显示任何内容

我得到的结果是-(基本上是打印accept button函数,而不是返回到Main函数的TName变量

TestName
India
25

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

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

转到按下Ok时调用accept方法的点,因此它是获取值的正确位置,因此它必须存储在变量中,然后在
get\u 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
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)
在main.py文件中,如果只按下了ok按钮,我会得到该值:

main.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
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)