ive创建了一个数据库,用于存储用户已添加到gui中的信息。然后,通过一个简单的函数返回该信息,以便在另一个类中使用该类,在该类中,我收到错误'All_Contents_Page' object has no attribute 'c'
“ All_Contents_Page”是类名。
class AddEmployee:
def __init__(self, master):
self.master = master
self.master.geometry("400x500")
self.master.title("Add Employee")
self.AddEmployee_ID = tk.Entry(self.master)
self.AddEmployee_FirstName = tk.Entry(self.master)
self.AddEmployee_Surname = tk.Entry(self.master)
self.AddEmployee_DOB = tk.Entry(self.master)
self.AddEmployee_Age = tk.Entry(self.master)
self.AddEmployee_Gender = tk.Entry(self.master)
self.AddEmployee_ID.grid(row = 1, column = 1)
self.AddEmployee_FirstName.grid(row = 2, column = 1)
self.AddEmployee_Surname.grid(row = 3, column = 1)
self.AddEmployee_DOB.grid(row = 4, column = 1)
self.AddEmployee_Age.grid(row = 5, column = 1)
self.AddEmployee_Gender.grid(row = 6, column = 1)
self.AddEmployee_ID_Label = tk.Label(self.master, text = "ID").grid(row = 1, column = 2 )
self.AddEmployee_FirstName_Label = tk.Label(self.master, text = "First Name").grid(row = 2, column = 2 )
self.AddEmployee_Surname_Label = tk.Label(self.master, text = "Surname").grid(row = 3, column = 2 )
self.AddEmployee_DOB_Label = tk.Label(self.master, text = "Date of Birth").grid(row = 4, column = 2 )
self.AddEmployee_Age_Label = tk.Label(self.master, text = "Age").grid(row = 5, column = 2 )
self.AddEmployee_Gender_Label = tk.Label(self.master, text = "Gender").grid(row = 5, column = 2 )
self.AddEmployee_Add_Button = tk.Button(self.master, text = "Add", command = self.AddEmployee_CheckInfo_Confirm).grid(row = 3, column = 3)
def AddEmployee_CheckInfo_Confirm(self):
if self.AddEmployee_FirstName.get() and self.AddEmployee_ID.get() and self.AddEmployee_Age.get() and self.AddEmployee_DOB.get() and self.AddEmployee_Gender.get() and self.AddEmployee_Surname != "":
if self.AddEmployee_FirstName.get() and self.AddEmployee_Surname.get():
print("ayyyy lmao")
self.AddEmployee_DatabaseRun()
self.AddEmployee_AddUserData()
self.AddEmployee_ReturnAllData()
else:
print("didnt work")
else:
print("Didnt work")
def AddEmployee_DatabaseRun():
self.conn = sqlite3.connect(':memory:')
self.c = self.conn.cursor()
self.c.execute("""CREATE TABLE Employees (
ID Integer,
FirstName text,
Surname text,
DOB integer,
Age integer,
Gender text
)""") #Creation of Database
self.conn.commit()
def AddEmployee_ReturnAllData(self):
self.c.execute("SELECT * FROM Employees")
return self.c.fetchall()
self.conn.commit() #Returned data
def AddEmployee_AddUserData(self):
self.c.execute("INSERT INTO Employees VALUES (:ID, :FirstName, :Surname, :DOB, :Age, :Gender)",
{'ID: ': self.AddEmployee_ID.get(), 'First Name: ': self.AddEmployee_FirstName.get() , 'Surname: ': self.AddEmployee_Surname.get() ,
'DOB: ': self.AddEmployee_DOB.get() , 'Age: ': self.AddEmployee_Age.get() , 'Gender: ': self.AddEmployee_Gender.get()})
self.conn.commit()
这是我尝试运行该函数以检索数据的类:
class All_Contents_Page:
def __init__(self, master):
self.master = master
self.master.geometry("1000x600")
self.master.title("Company Database")
self.All_Contents_Page_ListBox = tk.Listbox(self.master,width = 100, height = 20 ).grid(row = 1, column = 2)
self.All_Contents_Page_AddEmployeeButton = tk.Button(self.master, text = "Add Employee", command = self.gotoAddEmployee).grid(row = 2, column = 1)
self.OtherClassMethod = 'AddEmployee_ReturnAllData'
self.ReturnedData = getattr(AddEmployee, self.OtherClassMethod)
for data in enumerate(self.ReturnedData(self)):
self.All_Contents_Page_ListBox.insert(1, data)
对于为什么会出现此错误,我感到困惑,这是我尝试从类中调用函数的方式吗?
我决定不创建类的实例来获取功能,因为我将破坏正在创建实例的GUI。
解决这个问题最好是什么?
答案 0 :(得分:0)
getattr(AddEmployee, self.OtherClassMethod)(self)
在class All_Contents_Page
中可能是错误的,它不是AddEmployee
的子类。 AddEmployee.AddEmployee_ReturnAllData
可能正在尝试访问self.c
实例上的游标All_Contents_Page
,该实例在初始化期间没有收到c
成员。
目前尚不清楚您的意图,因此很难提出解决方案。但这可能是您描述的错误的原因。
答案 1 :(得分:0)
定义c
属性的唯一位置是AddEmployee_DatabaseRun
。但是,您不仅不会调用该方法,而且甚至不会创建AddEmployee实例以使其首先存在。传递给self
的{{1}}是AddEmployee_ReturnAllData
的当前实例-这是一个完全不同的类,也没有All_Contents_Page
属性。
尚不清楚您为什么期望c
存在,因为它仅在您不调用的一种方法中定义。也许您可以只在调用的方法中使用本地定义的连接?但实际上,您根本不应该编写这样的代码。