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.imagenLogo = PhotoImage(file="logo2.png")
		self.divLogo = Label(self.ventana1, image=self.imagenLogo), 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)
	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), y=130, width=200, height=30)
		self.formPasswordString = StringVar()
		self.formPassword = Entry(self.ventana1, textvariable=self.formPasswordString), y=185, width=200, height=30)
		botonAcceder = Button(self.ventana1, text="Acceder", command=self.login), y=240, width=150, height=30)
	def login(self):
		datos = (self.formUsuarioString.get(), self.formPasswordString.get())
Ventana = Aplicacion()

and then in the file I have that:

import pymysql
class conexiones:
	def conexion(self):
		conexion2 = pymysql.connect(host='null',
	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())

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/", line 1921, in __call__
    return self.func(*args)
  File "/Users/tomas/Downloads/DonMovil/", line 47, in login
  File "/Users/tomas/Downloads/DonMovil/", 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.


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.