每次触发信号时从 QLineEdit 获取不同的输入值

Getting different input values from QLineEdit each time I trigger the signal

我有一段代码(Info_box)

它获取输入值并使用另一段代码 (DataManipulation) 进行一些计算以生成包含所有请求项的数据框。

这是info_box.py:

from PyQt5 import QtCore, QtGui, QtWidgets
from DataManipulation import DataManipulation

class Ui_ContactCreator(object):
    def setupCreateContactUi(self, Form):
       Form.setObjectName("Form")
       Form.resize(426, 300)
       self.verticalLayoutWidget = QtWidgets.QWidget(Form)
       self.verticalLayoutWidget.setGeometry(QtCore.QRect(180, 20, 161, 241))
       self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
       self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
       self.verticalLayout.setContentsMargins(0, 0, 0, 0)
       self.verticalLayout.setObjectName("verticalLayout")

       self.QU_INITIAL = QtWidgets.QLineEdit(self.verticalLayoutWidget)
       self.QU_INITIAL.setObjectName("QU_INITIAL")

       self.verticalLayout.addWidget(self.QU_INITIAL)

       self.PRIX_VENTE_LT = QtWidgets.QLineEdit(self.verticalLayoutWidget)
       self.PRIX_VENTE_LT.setObjectName("PRIX_VENTE_LT")


       self.verticalLayout.addWidget(self.PRIX_VENTE_LT)


       self.PRIX_ACHAT_LT = QtWidgets.QLineEdit(self.verticalLayoutWidget)
       self.PRIX_ACHAT_LT.setObjectName("PRIX_ACHAT_LT")


       self.verticalLayout.addWidget(self.PRIX_ACHAT_LT)


       self.QU_ENTREE = QtWidgets.QLineEdit(self.verticalLayoutWidget)
       self.QU_ENTREE.setObjectName("QU_ENTREE")


       self.verticalLayout.addWidget(self.QU_ENTREE)


       self.QU_SORTIE = QtWidgets.QLineEdit(self.verticalLayoutWidget)
       self.QU_SORTIE.setObjectName("QU_SORTIE")


       self.verticalLayout.addWidget(self.QU_SORTIE)
       self.verticalLayoutWidget_2 = QtWidgets.QWidget(Form)
       self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(20, 29, 160, 221))
       self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
       self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
       self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
       self.verticalLayout_2.setObjectName("verticalLayout_2")
       self.QU_INITIAL_label = QtWidgets.QLabel(self.verticalLayoutWidget_2)
       self.QU_INITIAL_label.setObjectName("PRIX_VENTE_LT_label")
       self.verticalLayout_2.addWidget(self.QU_INITIAL_label)
       self.PRIX_VENTE_LT_label = QtWidgets.QLabel(self.verticalLayoutWidget_2)
       self.PRIX_VENTE_LT_label.setObjectName("PRIX_VENTE_LT_label")
       self.verticalLayout_2.addWidget(self.PRIX_VENTE_LT_label)
       self.PRIX_ACHAT_LT_label = QtWidgets.QLabel(self.verticalLayoutWidget_2)
       self.PRIX_ACHAT_LT_label.setObjectName("PRIX_ACHAT_LT_label")
       self.verticalLayout_2.addWidget(self.PRIX_ACHAT_LT_label)
       self.QU_ENTREE_label = QtWidgets.QLabel(self.verticalLayoutWidget_2)
       self.QU_ENTREE_label.setObjectName("QU_ENTREE_label")
       self.verticalLayout_2.addWidget(self.QU_ENTREE_label)
       self.QU_SORTIE_label = QtWidgets.QLabel(self.verticalLayoutWidget_2)
       self.QU_SORTIE_label.setObjectName("QU_SORTIE_label")
       self.verticalLayout_2.addWidget(self.QU_SORTIE_label)
       self.save_btn = QtWidgets.QPushButton(Form)
       self.save_btn.setGeometry(QtCore.QRect(314, 262, 101, 31))
       self.save_btn.setObjectName("save_btn")

       self.retranslateUi(Form)
       QtCore.QMetaObject.connectSlotsByName(Form)

       self.save_btn.clicked.connect(self.onButtonClicked)


    def onButtonClicked(self):

       DataManipulation(self.QU_INITIAL.text(), self.PRIX_VENTE_LT.text(), self.PRIX_ACHAT_LT.text(), self.QU_ENTREE.text(), self.QU_SORTIE.text())


    def retranslateUi(self, Form):
       _translate = QtCore.QCoreApplication.translate
       Form.setWindowTitle(_translate("Form", "Form"))
       self.QU_INITIAL_label.setText(_translate("Form", "Quantité initial"))
       self.PRIX_VENTE_LT_label.setText(_translate("Form", "Prix de vente"))
       self.PRIX_ACHAT_LT_label.setText(_translate("Form", "Prix d\'achat"))
       self.QU_ENTREE_label.setText(_translate("Form", "Quantité achetée"))
       self.QU_SORTIE_label.setText(_translate("Form", "Quantité vendue"))
       self.save_btn.setText(_translate("Form", "Enregistrer"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_ContactCreator()
    ui.setupCreateContactUi(Form)
    Form.show()
    sys.exit(app.exec_())

这是 DataManipulation.py:

import numpy as np
import pandas as pd



a = ''
x = ''
y = ''
z = ''
j = ''
def DataManipulation(val, val1,val2,val3,val4):
    global a, x, y, z, j
    try:
        a = float(val)
        x = float(val1)
        y = float(val2)
        z = float(val3)
        j = float(val4)
    except:
        print("Invalid Input")
        gazoil(a, x, y, z, j)
        

def gazoil(val, val1, val2, val3, val4):
    try:

        QU_INITIAL = val
        ARR_VENTE = np.array([])
        ARR_ACHAT = np.array([])
        ARR_ENTREE = np.array([])
        ARR_SORTIE = np.array([])
        ARR_STOCK = np.array([])
        ARR_MONTANT_VENTE = np.array([])
        ARR_MONTANT_ACHAT = np.array([])

    except:

        print("Invalid input")


    try:

        PRIX_VENTE_LT = val1
        ARR_VENTE = np.append(ARR_VENTE, PRIX_VENTE_LT)
        PRIX_ACHAT_LT = val2
        ARR_ACHAT = np.append(ARR_ACHAT, PRIX_ACHAT_LT)
        QU_ENTREE = val3
        ARR_ENTREE = np.append(ARR_ENTREE, QU_ENTREE)
        QU_SORTIE = val4
        ARR_SORTIE = np.append(ARR_SORTIE, QU_SORTIE)
        QU_STOCK = (val + val3) - (val4)
        ARR_STOCK = np.append(ARR_STOCK, QU_STOCK)


        MONTANT_VENTE = PRIX_VENTE_LT * QU_SORTIE
        ARR_MONTANT_VENTE = np.append(ARR_MONTANT_VENTE, MONTANT_VENTE)
        MONTANT_ACHAT = PRIX_ACHAT_LT * QU_ENTREE
        ARR_MONTANT_ACHAT = np.append(ARR_MONTANT_ACHAT, MONTANT_ACHAT)
        QU_INITIAL = QU_STOCK

    except:
        print('merci')

    frame = {'Prix de vente': ARR_VENTE, "Prix d'achat": ARR_ACHAT, "Quantité entrée": ARR_ENTREE,
         "Quantité sortie": ARR_SORTIE, "Quantité en stock": ARR_STOCK, "Montant Vente": ARR_MONTANT_VENTE,
         "Montant achat": ARR_MONTANT_ACHAT}
    global result

    result = pd.DataFrame(frame)

我想在每次触发信号 (save_btn) 时继续获取一组新的输入,并将前一个数据帧与新数据帧连接起来,以获得一个包含我所有数据的最终漂亮数据帧一直在插入,但似乎没有任何效果。我试图在 gazoil() 中设置一个循环,但是当我第一次点击 save_btn 时,界面会冻结并停止 运行。

为了根据输入进行一些计算,然后每次将它们添加到 DATAFRAME 中,我们可以执行以下操作。

首先我们定义我们将用来存储未来数据帧的数据帧。我们使用 None 来表明数据帧不存在。

 self.dataframe = None

然后在单击“保存按钮”按钮后,我们检查输入是否有效。如果不是,我们打印一个错误。否则我们将输入值和当前数据帧传递给函数 data_manipulation.

def save_btn_on_click(self):
    try:
        input_1 = float(self.line_edit_1.text())
        input_2 = float(self.line_edit_2.text())
        input_3 = float(self.line_edit_3.text())
    except:
        print("Invalid input!")
        return
    self.dataframe = data_manipulation(input_1, input_2, input_3, self.dataframe)
    print(f"{self.dataframe}\n")

data_manipulation中,我们进行基本计算,根据计算创建一个新的数据框,如果存在则将其连接到先前的数据框。

def data_manipulation(input_1, input_2, input_3, previous_dataframes):
    print(f"Received inputs: {input_1}, {input_2}, {input_3}\n")
    # Some calculation on current inputs
    input_1 = input_1 + 2
    input_2 = input_2 - 4
    input_3 = input_3 * 2

    # Creating current dataframe and concatenating to previous dataframes
    current_dataframe = pd.DataFrame([{'Calculated input 1': input_1, 'Calculated input 2': input_2, 'Calculated input 3': input_3}])

    if previous_dataframes is None:
        return current_dataframe
    else:
        return pd.concat([current_dataframe, previous_dataframes])

完整代码:

import sys

import pandas as pd
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, QLabel, QLineEdit


def data_manipulation(input_1, input_2, input_3, previous_dataframes):
    print(f"Received inputs: {input_1}, {input_2}, {input_3}\n")
    # Some calculation on current inputs
    input_1 = input_1 + 2
    input_2 = input_2 - 4
    input_3 = input_3 * 2

    # Creating current dataframe and concatenating to previous dataframes
    current_dataframe = pd.DataFrame([{'Calculated input 1': input_1, 'Calculated input 2': input_2, 'Calculated input 3': input_3}])

    if previous_dataframes is None:
        return current_dataframe
    else:
        return pd.concat([current_dataframe, previous_dataframes])



class App(QWidget):

    def __init__(self):
        # Reusable parts
        self.dataframe = None

        super().__init__()
        self.resize(400, 300)
        layout = QGridLayout()

        label_1 = QLabel("Input 1")
        self.line_edit_1 = QLineEdit()
        layout.addWidget(label_1, 0, 0)
        layout.addWidget(self.line_edit_1, 0, 1)

        label_2 = QLabel("Input 2")
        self.line_edit_2 = QLineEdit()
        layout.addWidget(label_2, 1, 0)
        layout.addWidget(self.line_edit_2, 1, 1)

        label_3 = QLabel("Input 3")
        self.line_edit_3 = QLineEdit()
        layout.addWidget(label_3, 2, 0)
        layout.addWidget(self.line_edit_3, 2, 1)

        button = QPushButton('Save Button', self)
        button.clicked.connect(self.save_btn_on_click)
        layout.addWidget(button, 3, 1)

        self.setLayout(layout)
        self.show()

    def save_btn_on_click(self):
        try:
            input_1 = float(self.line_edit_1.text())
            input_2 = float(self.line_edit_2.text())
            input_3 = float(self.line_edit_3.text())
        except:
            print("Invalid input!")
            return

        self.dataframe = data_manipulation(input_1, input_2, input_3, self.dataframe)
        print(f"{self.dataframe}\n")



if __name__ == '__main__':
    app = QApplication(sys.argv)
    example = App()
    sys.exit(app.exec_())