类实例中未定义“主”

时间:2018-07-18 23:11:13

标签: python-3.x tkinter instance tkinter-entry

我正试图传递一个类的实例,我可以向其检索函数,但是我继续收到运行该程序时未定义的“ 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()

1 个答案:

答案 0 :(得分:0)

您在这里遇到了一些主要问题,首先在出现错误的行上将master替换为self。您只能让其中一个班级成为主班。  然后,init的{​​{1}}函数被弄乱了。您基本上创建了两个递归错误。

假设您创建一个Database对象。那将创建一个新的Database对象,然后将其创建另一个MainPageGUI对象,该对象将创建另一个Database。这是递归错误之一

另一个递归错误发生在MainPageGUI类中。当您定义新的Database对象时,它将在其自身的Database函数中创建另一个init对象,这将创建另一个Database对象...

固定的Database应该看起来像这样:

init