我正试图传递一个类的实例,我可以向其检索函数,但是我继续收到运行该程序时未定义的“ master”。我在做什么错了?
class MainPageGUI:
def __init__(self, master):
self.master = master
self.master.title("Jans Corp")
self.master.configure(background='lightgrey')
self.master.geometry("1200x800")
self.DisplayData()
def DisplayData(self):
self.ReturnedData = Database(master) # <----------- I am recieving error here
self.ReturnedData = getattr(self.ReturnedData, 'ReadData')
for index, data in enumerate(self.ReturnedData):
tk.Label(self.master, text = data[0]).grid(row = index+1, column = 0)
tk.Label(self.master, text = data[1]).grid(row = index+1, column = 1)
tk.Label(self.master, text = data[2]).grid(row = index+1, column = 2)
tk.Label(self.master, text = data[3]).grid(row = index+1, column = 3)
tk.Label(self.master, text = data[4]).grid(row = index+1, column = 4)
tk.Label(self.master, text = data[5]).grid(row = index+1, column = 5)
tk.Label(self.master, text = data[6]).grid(row = index+1, column = 6)
这是即时通讯从
调用函数的地方class Database:
def __init__(self, master):
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("""CREATE TABLE Employees (
FirstName text,
Surname text,
Age Integer,
Postcode VARCHAR,
Wage Integer,
Email VARCHAR,
Hours Integer
)""")
conn.commit()
conn.close()
EmployeeInfo = MainPageGUI(master) <----------- Here i added master
db = Database()
db.addEmployee(EmployeeInfo)
def ReadData(self):
c.execute("SELECT * FROM Employees")
return c.fetchall()
def addEmployees(self , EmployeeInfo): #replace emp with the employees the users add from the entry function
with conn:
c.execute("INSERT INTO Employees VALUES (:FirstName, :Surname, :Age, :Postcode, :Wage, :Email, :Hours)",
{'FirstName':EmployeeInfo.FirstNameEntry.get(), 'Surname':EmployeeInfo.SurnameEntry.get(), 'Age':EmployeeInfo.AgeEntry.get(),
'postcode':EmployeeInfo.PostcodeEntry.get(), 'Wage':EmployeeInfo.EmailEntry.get(), 'Email':EmployeeInfo.EmailEntry.get(), 'Hours':EmployeeInfo.HoursEntry.get()})
conn.commit()
答案 0 :(得分:0)
您在这里遇到了一些主要问题,首先在出现错误的行上将master
替换为self
。您只能让其中一个班级成为主班。
然后,init
的{{1}}函数被弄乱了。您基本上创建了两个递归错误。
假设您创建一个Database
对象。那将创建一个新的Database
对象,然后将其创建另一个MainPageGUI
对象,该对象将创建另一个Database
。这是递归错误之一
另一个递归错误发生在MainPageGUI
类中。当您定义新的Database
对象时,它将在其自身的Database
函数中创建另一个init
对象,这将创建另一个Database
对象...
固定的Database
应该看起来像这样:
init