Function writing values more than once - Pyqt5

Asked

Viewed 38 times

-1

Hello

I’m using pyqt5 to create the graphical interface of a supposed clothing store (I’m training), I have the fields to receive the data to register each type of clothing, but when clicking the sign up button it saves the correct data in the txt file, but when registering a second piece he registers twice the same thing, if he registers a third piece he registers 4 and so on, does anyone know what it might be? Code below.

from PyQt5 import uic,QtWidgets


def CadastrarProdutos():
    cadastrar.show()
    description = cadastrar.lineEdit.text()
    price = cadastrar.lineEdit_2.text()
    amount = cadastrar.lineEdit_3.text()
    dados = description, price, amount
    cadastrar.pushButton.clicked.connect(SalvarDados_cadastro)
    return dados

def SalvarDados_cadastro():
    dados_save = CadastrarProdutos()
    print(dados_save)
    db = open("Estoque.txt","a+")
    dados_formatados = ("|".join(dados_save)) + "\n"
    db.write(dados_formatados)
    db.close()
    
try:
   with open('Estoque.txt', 'r') as f:
       pass
except IOError:
    create_db = open("Estoque.txt","a+")
    create_db.write("Descricao|Preco|Quantidade\n")
    create_db.close()
    
app=QtWidgets.QApplication([])
menu=uic.loadUi("menu_button.ui")
menu.pushButton.clicked.connect(CadastrarProdutos)
cadastrar=uic.loadUi("cadastro_button.ui")

menu.show()
app.exec()

The data is saved as shown below after I try to register the following items: T-shirt, pink T-shirt and short pink T-shirt with their respective values and quantities.

('Camiseta', '15', '10')
('Camiseta Rosa', '16', '4')
('Camiseta Rosa', '16', '4')
('Camiseta Rosa curta', '14', '26')
('Camiseta Rosa curta', '14', '26')
('Camiseta Rosa curta', '14', '26')
('Camiseta Rosa curta', '14', '26')

I need to store only 1 at a time, can anyone tell me where I’m going wrong? Thanks.

1 answer

1

Hey, I couldn’t run your code, but here’s an example that does what you need:

import sys
import os
from PyQt5.QtWidgets import *


class TelaPrincipal(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(TelaPrincipal, self).__init__(*args, **kwargs)
        self.setWindowTitle('Tela Principal')
        self.setGeometry(100, 200, 300, 200)

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        self.layout = QVBoxLayout(self.central_widget)

        self.description = QLineEdit()
        self.price = QLineEdit()
        self.amount = QLineEdit()

        self.cadastrar = QPushButton('Salvar Dados')
        self.cadastrar.clicked.connect(self.salvar_dados_cadastro)

        self.layout.addWidget(self.description)
        self.layout.addWidget(self.price)
        self.layout.addWidget(self.amount)
        self.layout.addWidget(self.cadastrar)

        self.setLayout(self.layout)

        self.check_txt_file()

    def check_txt_file(self):
        exist = os.path.exists('Estoque.txt')
        if exist is False:
            create_db = open("Estoque.txt", "a+")
            create_db.write("Descricao|Preco|Quantidade\n")
            create_db.close()

    def cadastrar_produtos(self):
        dados = f'{self.description.text()}, {self.price.text()},
                  {self.amount.text()}\n'
        return dados

    def salvar_dados_cadastro(self):
        dados = self.cadastrar_produtos()

        with open('Estoque.txt', 'a+') as w:
            w.write(dados)


def main():
    app = QApplication(sys.argv)
    root = TelaPrincipal()
    root.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.