尝试在tkinter GUI上显示数据库数据时当前收到错误。错误是Method is not iterable
,我知道错误的含义,但是不知道如何解决。
这是我要从中返回所有信息的类:
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()
相当简单,这是我试图在按下按钮时实际显示数据的地方:
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)
答案 0 :(得分:2)
您必须调用ReadData
方法:
# 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