Problem with OOP classes and functions

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:

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:

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()

Copy Code
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!

Hi @tomassanchezgarcia, you’re not returning anything from the conexion() method – you’re just assigning the connection to a variable and then do nothing with it. So try this instead:

def connexion(self):
    return pymysql.connect(
        # ...
    )
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.