0

Currently receiving an error when trying to display Database data on tkinter GUI. The error is Method is not iterable, i get what the error means but have no idea as to how i would go about fixing it.

Here is the class i am returning all the infromation from:

class Database:
    def __init__(self, master):
        self.master = master

        self.conn = sqlite3.connect(':memory:')
        self.c = self.conn.cursor()

        self.c.execute("""CREATE TABLE Employees (
                        FirstName text,
                        Surname text,
                        Age Integer,
                        Postcode VARCHAR,
                        Wage Integer,
                        Email VARCHAR,
                        Hours Integer
                        )""") #Creation of Database

        self.conn.commit()

        self.addEmployees() # running the function, need to fix this, Only run when "add employee" button is pressed!


    def ReadData(self):
        self.c.execute("SELECT * FROM Employees")
        return self.c.fetchall() # this reads and returns all the data.

    def addEmployees(self): # This function adds all the info the users have put into the entry in new EmployeeEmail

        self.c.execute("INSERT INTO Employees VALUES (:FirstName, :Surname, :Age, :Postcode, :Wage, :Email, :Hours)",
                     {'FirstName':"Sharjeel" , 'Surname':"Jan" , 'Age':"21" ,
                     'Postcode':"aa" , 'Wage':"1220000" , 'Email':"aa" , 'Hours':"230"})

        self.conn.commit()

Fairly simple, here is where im trying to actually display the data when a button is pressed:

class MainPageGUI:
    def __init__(self, master):

        self.master = master
        self.master.title("Jans Corp")
        self.master.configure(background='lightgrey')
        self.master.geometry("1200x800")



    def DisplayData(self):
            self.TheData = Database(self.master)
            self.ReturnedData = getattr(self.TheData, 'ReadData') <------ Here is where i am receiving the error.
            for data in enumerate(self.ReturnedData):
                tk.Label(self.master, text = data[0]).grid(row = 1, column = 0)
                tk.Label(self.master, text = data[1]).grid(row = 1, column = 1)
                tk.Label(self.master, text = data[2]).grid(row = 1, column = 2)
                tk.Label(self.master, text = data[3]).grid(row = 1, column = 3)
                tk.Label(self.master, text = data[4]).grid(row = 1, column = 4)
                tk.Label(self.master, text = data[5]).grid(row = 1, column = 5)
                tk.Label(self.master, text = data[6]).grid(row = 1, column = 6)
6
  • does print(type(self.ReturnedData)) say its a method? Commented Jul 19, 2018 at 17:13
  • Can you please copy and paste the error from IDLE please? And also, the guy above asked my second question. Commented Jul 19, 2018 at 17:13
  • 1
    You didn't call the method: use self.ReturnedData = getattr(self.TheData, 'ReadData')() (note the () at the end) Commented Jul 19, 2018 at 17:14
  • 3
    Any particular reason you're using getattr here? Why not just do self.TheData.ReadData()? Commented Jul 19, 2018 at 17:15
  • @JacobIRR yes it says <class 'method'> and @Swift it says "File "C:\Users\Sharjeel Jan\Desktop\Database System\The System\New Project1.py", line 200, in DisplayData for data in enumerate(self.ReturnedData): TypeError: 'method' object is not iterable" Commented Jul 19, 2018 at 17:17

1 Answer 1

2

You have to call the ReadData method:

# ReadData is a method, so call it:
self.ReturnedData = getattr(self.TheData, 'ReadData')()  # note the extra '()'
# Or, even more obviously, as pointed out in the comments
# self.ReturnedData = self.TheData.ReadData()
for data in enumerate(self.ReturnedData):  # I think this line threw the error
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.