Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello good to all

I am trying to login verifying the username and password. If I do it normal without OOP it works for me and I do it well. The thing is that I am getting into OOP and I want to do it in OOP but I more or less manage to link classes and functions. But I have a bug here where it doesn't read the cursor, and I have it defined

I have the main file:


Python
from tkinter import *
from tkinter import ttk
import pymysql
from tkinter import messagebox as MessageBox
from tkinter import scrolledtext as st
import conexion
 
class Aplicacion:
	def __init__(self):
		self.conexion1 = conexion.conexiones()
		self.ventana1 = Tk()
		self.ventana1.title("Login")
		self.ventana1.geometry("400x400")
		self.imagenLogo = PhotoImage(file="logo2.png")
		self.divLogo = Label(self.ventana1, image=self.imagenLogo)
		self.divLogo.place(x=93, y=0)
		self.x_ventana = self.ventana1.winfo_screenwidth() // 2 - 300 // 2
		self.y_ventana = self.ventana1.winfo_screenheight() // 2 - 300 // 2
		self.posicion = str(300) + "x" + str(300) + "+" + str(self.x_ventana) + "+" + str(self.y_ventana)
		self.ventana1.geometry(self.posicion)
		self.ventana1.resizable(0,0)
 
		self.formulario()
 
 
 
		self.ventana1.mainloop()
 
	def formulario(self):
		ttk.Label(text="Usuario:").place(x=50, y=110)
		ttk.Label(text="Contraseña:").place(x=50, y=165)
 
		self.formUsuarioString = StringVar()
		self.formUsuario = Entry(self.ventana1, textvariable=self.formUsuarioString)
		self.formUsuario.place(x=50, y=130, width=200, height=30)
 
		self.formPasswordString = StringVar()
		self.formPassword = Entry(self.ventana1, textvariable=self.formPasswordString)
		self.formPassword.place(x=50, y=185, width=200, height=30)
 
		botonAcceder = Button(self.ventana1, text="Acceder", command=self.login)
		botonAcceder.place(x=75, y=240, width=150, height=30)
 
	def login(self):
		datos = (self.formUsuarioString.get(), self.formPasswordString.get())
 
		self.conexion1.verificar(datos)
 
 
 
 
 
Ventana = Aplicacion()


and then in the connection.py file I have that:


Python
import pymysql
 
class conexiones:
	def conexion(self):
		conexion2 = pymysql.connect(host='null',
                                 user='myadmin',
                                 password='null',
                                 database='python',
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)
 
	def verificar(self, datos):
		con1 = self.conexion()
		cur = con1.cursor()
		sql = "SELECT * FROM trabajador WHERE nombre='{}' AND password='{}'".format(self.formUsuarioString.get(), self.formPasswordString.get())
		cur.execute(sql)
		comprobardatosAcceso=cur.fetchall()


and this is the error it gives me when I try to do the query:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
    return self.func(*args)
  File "/Users/tomas/Downloads/DonMovil/objetos.py", line 47, in login
    self.conexion1.verificar(datos)
  File "/Users/tomas/Downloads/DonMovil/conexion.py", line 14, in verificar
    cur = con1.cursor()
AttributeError: 'NoneType' object has no attribute 'cursor'
[Finished in 12.2s]


Where can be the error?

Any additional information would be appreciated.

Greetings!


What I have tried:

Where can be the error?

Any additional information would be appreciated.
Posted
Updated 22-Jan-22 12:31pm
Comments
Richard Deeming 24-Jan-22 8:28am    
You are also storing your users' passwords in plain text. Don't do that!

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

Your code is wrong:
Python
def conexion(self):
    conexion2 = pymysql.connect(host='null',
                             user='myadmin',
                             password='null',
                             database='python',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

def verificar(self, datos):
    con1 = self.conexion()

The The call to con1 = self.conexion() is expecting to receive a return value. But your conexion method does not return anything, so con1 is a NoneType

You need to add the following at the end of the conexion method:
Python
return conexion2

You should also add some error checks to ensure that MySQL returns a valid connection in the first place.
 
Share this answer
 
Thank you very much, it was because that line was missing.

Thank you very much for your help.
 
Share this answer
 
Comments
Richard Deeming 24-Jan-22 8:27am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

Do not post your comment as another "solution" to your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900