【问题标题】:How to return variables from PyQt5 UI to Main function - Python如何将变量从 PyQt5 UI 返回到 Main 函数 - Python
【发布时间】:2018-10-01 14:08:40
【问题描述】:

我已经使用 pyqt5 设计了一个自定义的表单布局 UI,并且希望将变量导入回主函数以进一步执行主函数。

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

你能指导我吗,我怎样才能从 pyqt5 formlayout ui 中获取变量到 main 函数 -

这里是 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 函数..?

【问题讨论】:

    标签: python pyqt pyqt5 qdialog


    【解决方案1】:

    首先,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
    

    如果只按下 ok 按钮,我会在文件 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)
    

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2018-12-26
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      相关资源
      最近更新 更多