Insert data from a function (OOP)

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/", 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:

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

What am I doing wrong?

Any additional information would be appreciated.


Hi @tomassanchezgarcia, your crear_cliente() method expects a datos argument but you’re passing it as the command callback to a button, which again will be called without any arguments. Do you even need that datos parameter? You’re declaring a variable of the same name right in the first line of the method, overriding the argument…

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