Connect a lineEdit to a class method

Asked

Viewed 33 times

0

am having a problem giving the following message "Typeerror: query'"

I have a project that so far works well in structured paradigm, but I’m trying to rewrite the code in POO.

I have the following py file " connected" with the following code:

import mysql.

# Classe responsável pela conexão ao banco de dados

class Conexao():
    def __init__(self):
        try:
            self.conectar = mysql.connector.connect(host="localhost", user="root", database="db_farmacia", password="")
        except mysql.connector.Error as e:
            print(e)
        return self.conectar

    def ConsultarTabela(self, consulta):
        try:
            self.cursor = self.conexao_db()
            lista = self.cursor.execute("SELECT nome_produto, preço FROM tb_produtos WHERE codigo=?;", [consulta])
            return lista.fetchall()

        except mysql.connector.Error as e:
            print(e)
        return self.conectar

And the following py file with the following code:

from PyQt5 import uic, QtWidgets, QtCore, QtGui
from conexao import Conexao

#Função que insere código

# OBS: inserir_codigo = é um linedEdit


class frenteCaixa:

    def consultaCodigo(self):

        codigo = pdv.inserir_codigo.text()
        pdv.inserir_codigo.setText("")
        consultados = Conexao()
        busca = consultados.ConsultarTabela(codigo)
        print(busca)

app = QtWidgets.QApplication([])
pdv = uic.loadUi("interface_2.ui")
tela_total = uic.loadUi("interface_total.ui")
dial_forma_pgto = uic.loadUi("dialogo_forma_pgto.ui")

pdv.inserir_codigo.returnPressed.connect(frenteCaixa.consultaCodigo) # Utiliza o enter para enviar o código sem necessidade do pushButtom
pdv.showFullScreen()
pdv.show()
app.exec()

My error is in the code : pdv.inserir_codigo.returnPressed.connect(frentCaixa.consultaCodigo). In the structured code is : pdv.inserir_codigo.returnPressed.connect(funcao_1) and works perfectly. In what I would be missing?

Grateful for the help!

1 answer

0

as its class is with the attribute self it needs to be instantiated before using you can instantiate and call the query methodCodigo() or turn your method into a class method

# 1 solução
pdv.inserir_codigo.returnPressed.connect(frenteCaixa().consultaCodigo)

# 2 solução
@classmethod
def consultaCodigo(cls):
     #seu codigo

pdv.inserir_codigo.returnPressed.connect(frenteCaixa.consultaCodigo)
  • Hi Jooj, thanks for the reply and for the help, the solution 1 no longer makes lock, but I can not send too, by pressing enter on lineEdit, he should send the code, this is not working. I was able to function in solution 2, but without importing the connection module, I included the query in the database and fetchall in the same code and with the class method it worked! Thank you very much, then I need to rework the module "Connection".

  • In any case your comment has already taught a lot about POO!

Browser other questions tagged

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