简体   繁体   English

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

[英]How to return variables from PyQt5 UI to Main function - Python

I have design a customized formlayout ui using pyqt5 and want to import variables back to the main function for further execution of the main function.我已经使用 pyqt5 设计了一个自定义的 formlayout ui,并希望将变量导入回主函数以进一步执行主函数。

I have tried many ways to get the return values from the main function when the "OK" button has clicked but unable to get the variables from the main function.当单击“确定”按钮但无法从主函数获取变量时,我尝试了多种方法来从主函数获取返回值。

Can you please guide me, how can i get the variables from the pyqt5 formlayout ui to main function -你能指导我吗,我怎样才能从 pyqt5 表单布局 ui 中获取变量到主函数 -

Here is the Code of PyQt5 FormLayout UI function -这是 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)

Main Function is -主要功能是——

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

I am unable to get the desired values to the output function.我无法获得输出函数所需的值。 Even i have used the getoutput function to return the values and get the output to "TName".即使我已经使用 getoutput 函数返回值并将输出获取到“TName”。 But i am not able to get the value into the TName variable and nothing is displaying.但是我无法将值放入 TName 变量中,并且没有显示任何内容。

The Result i am getting is - (which is basically printing the accept button function but not the TName variable which is returned to Main function.我得到的结果是 - (它基本上是打印接受按钮函数,而不是返回到 Main 函数的 TName 变量。

TestName
India
25

How can i get the return values from PyQt5 Formlayout UI function to Main function..?如何从 PyQt5 Formlayout UI 函数获取返回值到 Main 函数..?

In the first place, FormLayout is a layout, that is, a class that is responsible for positioning the widgets within a window, so it is irrelevant for these cases.首先,FormLayout 是一个布局,即负责在窗口内定位小部件的类,因此与这些情况无关。 On the other hand, closeEvent() should never be invoked, that is a function that serves to handle the closed window event.另一方面,不应调用closeEvent() ,这是一个用于处理关闭窗口事件的函数。

Going to the point the accept method is called when Ok is pressed, so it is the right place to get the values so it must be stored in a variable, and then returned in the get_output() method:转到当按下 Ok 时调用 accept 方法的点,因此它是获取值的正确位置,因此必须将其存储在变量中,然后在get_output()方法中返回:

pyformlayout.py 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

And in the file main.py I get the value if only the ok button has been pressed:在文件 main.py 中,如果仅按下 ok 按钮,我会得到值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM