Problem with 'if' statement in OOP

Good,
I’m having a problem trying to close one window while opening the other. What I have works well, if the fields are not filled in, the message pops up, if they are correct the other new window appears and the old one closes.

The problem is that if I fill in the fields and they are incorrect data, the first window closes. and I don’t know how to make a condition to verify that they are incorrect data.

I am doing it in object-oriented programming, so I don’t know how to verify the data entered in one file and in another.

In the main file I have this:

def login(self):
		datos = (self.formUsuarioString.get(), self.formPasswordString.get())
		if self.formUsuarioString.get() == "" or self.formPasswordString.get() == "":
			MessageBox.showinfo("INFORMACION", "Debes rellenar todos los campos.")
		else:
			self.ventana1.destroy()
			self.conexion1.verificar(datos)

and in the other file:

def verificar(self, datos):
		con1 = self.conexion()
		cur = con1.cursor()
		sql = "SELECT * FROM trabajador WHERE nombre=%s AND password=%s"
		cur.execute(sql,datos)
		comprobardatosAcceso=cur.fetchall()
		if comprobardatosAcceso:
			print ("hay registro")
			for i in comprobardatosAcceso:
				print("Hola: " + i['nombre'] + " con email: " + i['email'])

			self.ventana2 = Tk()
			self.ventana2.title("Hola: " + i['nombre'])
			self.ventana2.geometry("400x400")

			self.ventana2.mainloop()

		else:
			print("error")
			MessageBox.showinfo("INFORMACION", "No existe ningun usuario asi")

How would be the solution to verify the data entered through object-oriented programming.

any additional information would be appreciated.

Greetings!

Hi, tomassanchezgarcia

Make your verificar function to return true or false depending on the verification result:

Then use it to decide if you should close current window:

if self.conexion1.verificar(datos):
    self.ventana1.destroy()

I would also move any logic which doesn’t relate with verification itself to outside of this function. This will make your code cleaner. Learn more about Single-responsibility principle.

1 Like

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