-4
\$\begingroup\$

I am learning to write my codes accordingly with functions and classes. My original code can create a graphic interface that interacts with different functions. As I said before, I am trying to write it better and I do not know the correct way. I leave the code to correct to see if someone can help me to execute it. I clarify that I work with Python 2.7.

At the momment I have this error:

TypeError: __init __ () takes exactly 1 argument (2 given).

# coding=utf-8
# encoding=utf8
"""..."""
import sys
from tkinter import*
import tkinter as tk
import csv
import pandas as pd
import os
reload(sys)
sys.setdefaultencoding('utf8')


class Shark:
    """."""

    def __init__(self):
        """."""
        self.b = "Working.csv"
        """Nueva Ventana."""
        global ventana
        self.ventana = Tk()
        """Dimenciones de la ventana."""
        self.ventana.geometry("1100x900+0+0")
        """Titulo."""
        self.ventana.title("CSV TOOLS")
        """Texto."""
        self.lblUsuario = Label(text="Bienvenidos a CSV Tools", font=("Agency FB", 22)).place(x=10, y=10)
        """Cargar CSV."""
        self.lblUsuario = Label(text="Cargar CSV", font=("Agency FB", 14)).place(x=10, y=100)
        """Campo de texto."""
        self.entradaU = StringVar()
        self.txtUsuario = Entry(ventana, textvariable=entradaU).place(x=10, y=150)
        self.aceptar = Button(ventana, text="   Aceptar   ", command=self.aceptar, font=("Agency FB", 10)).place(x=210, y=148)

    def aceptar(self):
        """."""
        global a
        self.a = entradaU.get()
        if self.a == "exit":
            raise SystemExit
        else:
            pass
        try:
            with open(self.a, 'rb') as f:
                self.reader = csv.reader(f)
                global lista2
                self.lista2 = list(reader)
                self.lstmaterias = Label(ventana, text="Cabeceras:                                  ").place(x=10, y=180)
                self.lstmaterias = Listbox(ventana, width=100, height=10)
                global lista22
                self.lista22 = ' '.join(str(e) for e in lista2[0])
                self.lstmaterias.insert(0, lista22)
                self.lstmaterias.place(x=10, y=200)
                global entrada2
                self.entrada2 = StringVar()
                self.lstmaterias222 = Label(ventana, text="Filtro").place(x=10, y=400)
                global txtusuarijjo
                self.txtusuarijjo = Entry(ventana, textvariable=entrada2).place(x=10, y=420)
                self.filtro = Button(ventana, text="   Aceptar   ", command=orden, font=("Agency FB", 10)).place(x=210, y=420)

        except IOError:
            lstmaterias = Label(ventana, text="Error:").place(x=10, y=180)
            lstmaterias = Label(ventana, text="Archivo No Encontrado").place(x=40, y=180)

    def orden(self):
        """."""
        global a_orden
        self.a_orden = entrada2.get()
        if self.a_orden == "exit":
            raise SystemExit
        if len(entrada2.get()) == 0:
            return self.a_orden
        if self.a_orden not in self.lista22:
            self.a_orden

        else:
            if self.a_orden in self.lista22:
                self.entrada3 = StringVar()
                self.lstmaterias = Label(self.ventana, text="Ascendente:").place(x=350, y=400)
                self.lstmaterias = Label(self.ventana, text="(True - Flase)").place(x=350, y=450)
                self.txtusuario = Entry(self.entana, textvariable=entrada3).place(x=350, y=420)
                self.true_false = Button(self.ventana, text="   Aceptar   ", command=self.valores, font=("Agency FB", 10)).place(x=550, y=420)
                global f
                self.f = entrada3.get()
                global entrada4
                self.entrada4 = StringVar()
                self.lstmaterias = Label(self.ventana, text="Cantidad de datos:").place(x=700, y=400)
                self.txtusuario = Entry(self.ventana, textvariable=self.entrada4).place(x=700, y=420)
                self.true_false = Button(self.ventana, text="   Aceptar   ", command=self.top, font=("Agency FB", 10)).place(x=900, y=420)

    def top(self):
        """."""
        self.nn = self.entrada4.get()
        self.nn = int(self.nn)
        self.movies = self.pd.read_csv(self.b, nrows=self.nn)
        self.movies = self.movies.to_csv(b, index=False)

    def valores(self):
        """."""
        self.movies = self.pd.read_csv(self.a, error_bad_lines=False, delimiter=',', encoding="utf-8-sig", low_memory=False)
        self.movies = self.movies.sort_values(self.a_orden, ascending=f)
        self.movies = self.movies.to_csv(self.b, index=False)
        global entrada5
        self.entrada5 = StringVar()
        self.lstmaterias = Label(self.ventana, text="Nombre del Archivo Nuevo:").place(x=10, y=500)
        self.txtusuario = Entry(self.ventana, textvariable=self.entrada5).place(x=10, y=520)
        self.true_false = Button(self.ventana, text="   Aceptar   ", command=self.new_csv, font=("Agency FB", 10)).place(x=210, y=520)
        global entrada6
        self.entrada6 = StringVar()
        self.lstmaterias2 = Label(self.ventana, text="Introduzca las Cabeceras:").place(x=350, y=500)
        self.txtusuario2 = Entry(self.ventana, textvariable=entrada6).place(x=350, y=520)
        self.true_false2 = Button(self.ventana, text="   Aceptar   ", command=cabex, font=("Agency FB", 10)).place(x=550, y=520)

    def new_csv(self):
        """."""
        global a_valor
        self.a_valor = entrada5.get()
        if self.a_valor == "exit":
            raise SystemExit

    def cabex(self):
        """."""
        global a_cabex
        self.a_cabex = self.entrada6.get()
        self.a_cabex = self.a_cabex.split()
        with open(self.b, 'r') as self.csvfile, open(self.a_valor, "w") as self.output:
            self.reader = self.csv.DictReader(self.csvfile)
            self.writer = self.csv.DictWriter(self.output, fieldnames=self.a_cabex, extrasaction='ignore')
            self.writer.writeheader()
            for self.row in self.reader:
                self.a = 'fillRate'
                if self.a in self.lista2[0]:
                    self.row['fillRate'] = '{:.2f}'.format(float(self.row['fillRate']) * 100)
                self.writer.writerow(row)
        self.os.remove(b)

        self.lstmsss = Label(self.ventana, text="Reporte Creado", font=("Agency FB", 30)).place(x=350, y=600)


def main():
    """Set name of Shark object."""
    sammy = Shark("Martin Bouhier")
    sammy.aceptar()
    sammy.orden()
    sammy.top()
    sammy.valores()
    sammy.new_csv()
    sammy.cabex()
    ventana.mainloop()


if __name__ == "__main__":
    main()

input()
\$\endgroup\$

closed as off-topic by Ludisposed, alecxe, 409_Conflict, t3chb0t, Jamal Dec 16 '17 at 6:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Ludisposed, alecxe, 409_Conflict, t3chb0t, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ This will be hard to review for some, since not all of us are Spanish speakers, \$\endgroup\$ – Ludisposed Dec 15 '17 at 17:44
  • 3
    \$\begingroup\$ "For now I have this error: TypeError:" Broken code is off topic \$\endgroup\$ – Ludisposed Dec 15 '17 at 17:47
  • \$\begingroup\$ @Ludisposed the comments are not necessary to understand the code \$\endgroup\$ – Martin Bouhier Dec 15 '17 at 17:49
  • \$\begingroup\$ Your "code" is necessary to understand the code ==> global ventana I don't know what this is \$\endgroup\$ – Ludisposed Dec 15 '17 at 17:51
  • \$\begingroup\$ Ventana is a variable... global is to use it in another functions! \$\endgroup\$ – Martin Bouhier Dec 15 '17 at 17:52
0
\$\begingroup\$

1) When you define __init__ method of a class in Python, you automatically have self argument, which is not written explicitly in function call (e.g. look at documentation example).

To specify the argument the way you want, you need to make __init__(self, shark_name) and call it as sammy = Shark("Martin Bouhier").

Note that this argument shark_name will be accessible in __init__ constructor, so you can save it in field, for example (self.name = shark_name).

2) You have another problem:

self.txtUsuario = Entry(ventana, textvariable=entradaU).place(x=10, y=150)

is to be replaced with

self.txtUsuario = Entry(ventana, textvariable=self.entradaU).place(x=10, y=150)

You have no local variable entradaU, only a field with such name. The same goes for entrada3 and others - the interpreter will tell you.

3) You mix up field and local variable (namely, ventana). Every object of Shark has its own instance of ventana, which can have different values. If you want a static variable in your class, you should define @staticmethod setter and getter for this field.

4) I suggest you work on your code style as well: pay special attention to PEP - classical code style guide for Python. Furthermore, I see no meaning in comments like """.""", field named b in Shark class, true_false Button looks very confusing, etc. I suggest you google some best practices for Python.

5) As mentioned earlier, you would greatly benefit if your code had been purely English: many developers in the world would find your code clearer. Not to mention all employers and possible colleagues who can only speak English - you will face the necessity of good written English (including code) in very close future.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.