Click here to Skip to main content
15,302,851 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I'm trying to insert a client to the database with OOP, but I get this error:

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)
TypeError: conexiones.crear_cliente() missing 1 required positional argument: 'datos'

And if I remove the data variable from the create_customer function, it doesn't give me any errors, but it doesn't insert the data in the database.

I have this:

Python
def formulario_crear_cliente(self):
		self.pagina1 = ttk.Frame(self.cuaderno1)
		self.cuaderno1.add(self.pagina1, text="Crear cliente")
		self.labelframe1 = ttk.LabelFrame(self.pagina1, text="Registrar cliente")
		self.labelframe1.grid(column=0, row=0, padx=5, pady=10)

		self.label1 = ttk.Label(self.labelframe1, text="Nombre:")
		self.label1.grid(column=1, row=0, padx=4, pady=4)
		self.crearNombre = StringVar()
		self.entrycrearNombre = Entry(self.labelframe1, textvariable=self.crearNombre)
		self.entrycrearNombre.grid(column=2, row=0, padx=4, pady=4)

		self.label2 = ttk.Label(self.labelframe1, text="Apellidos:")
		self.label2.grid(column=3, row=0, padx=4, pady=4)
		self.crearApellidos = StringVar()
		self.entrycrearApellidos = Entry(self.labelframe1, textvariable=self.crearApellidos)
		self.entrycrearApellidos.grid(column=4, row=0, padx=4, pady=4)

		self.label3 = ttk.Label(self.labelframe1, text="D.N.I:")
		self.label3.grid(column=1, row=1, padx=4, pady=4)
		self.crearDni = StringVar()
		self.entrycrearDni = Entry(self.labelframe1, textvariable=self.crearDni)
		self.entrycrearDni.grid(column=2, row=1, padx=4, pady=4)

		self.label4 = ttk.Label(self.labelframe1, text="Teléfono:")
		self.label4.grid(column=3, row=1, padx=4, pady=4)
		self.crearNumero = StringVar()
		self.entrycrearNumero = Entry(self.labelframe1, textvariable=self.crearNumero)
		self.entrycrearNumero.grid(column=4, row=1, padx=4, pady=4)

		self.label5 = ttk.Label(self.labelframe1, text="Direccion:")
		self.label5.grid(column=1, row=2, padx=4, pady=4)
		self.crearDireccion = StringVar()
		self.entrycrearDireccion = Entry(self.labelframe1, textvariable=self.crearDireccion)
		self.entrycrearDireccion.grid(column=2, row=2, padx=4, pady=4)

		self.boton1 = ttk.Button(self.labelframe1, text="Crear cliente", command=self.crear_cliente)
		self.boton1.grid(column=4, row=6, padx=4, pady=4)

	def crear_cliente(self, datos):
		datos = (self.crearNombre.get(), self.crearApellidos.get(), self.crearDni.get(), self.crearNumero.get(), self.crearDireccion.get())
		con2 = self.conexion()
		cur2 = con2.cursor()
		sql2 = "INSERT INTO clientes(nombre, apellidos, dni, numero, direccion) VALUES (%s, %s, %s, %s, %s)"
		cur2.execute(sql2,datos)
		crear_cliente=cur2.fetchall()


What I have tried:

What am I doing wrong? Any additional information would be appreciated.

Greetings!
Posted
Updated 24-Jan-22 6:47am
v2

1 solution

The error message is clear. Look at your function definition:
Python
def crear_cliente(self, datos):

So, when you call this function you need to pass it a parameter of whatever datos is supposed to represent. However, I notice in your function that the first line is:
Python
datos = (self.crearNombre.get(), self.crearApellidos.get(), self.crearDni.get(), self.crearNumero.get(), self.crearDireccion.get())

So why do you need an input parameter in the first place?

Note that as with your other questions, this has nothing to do with OOP, but everything to do with function definitions, and parameter passing.
   
Comments
Tomas Sanchez Garcia 25-Jan-22 11:32am
   
Thank you very much for your answer, I just did what you told me.

Thanks for your time. and sorry for the inconvenience caused.

I thought it had something to do with it, in the sense that everything had to go together.

Now I'm understanding things a little more.


Thanks again.

Greetings!
Richard MacCutchan 25-Jan-22 11:58am
   
Your are welcome. And no inconvenience caused, we all do this because we want to help.

I would recommend you take a look at The Python Tutorial — Python 3.9.10 documentation[^], as it has lots of useful information.

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