Skip to main content
 首页 » 编程设计

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

2024年12月31日28sky-heaven

我使用 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 中,如果仅按下 ok 按钮,我将获得值:

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