-3

Im trying to pass an instance of an class to which i can retrieve a function, however i keep on recieving "master" not defined when i run the program. What am i doing wrong?

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)

Here is where im using calling the function from

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()
hiihihihelloo
  • 99
  • 2
  • 8
  • In that function there is no `master`. There is, however, `self.master`. Either pass it in as a parameter, or use the attribute. – Bryan Oakley Jul 18 '18 at 23:30

1 Answers1

0

You got some major problems here, first replace master with self on the line where you got the error. You can only have one of the classes be the master. Then the init function for Database is all messed up. You basically created two recursion errors.

Say you create a Database object. That would create a new MainPageGUI object, which would then go and create another Database object, which would create another MainPageGUI. That's one of the recursion errors

The other recursion error happens in the Database class. When you define a new Database object it will, in its own init function, create another Database object, which will create another Database object...

A fixed init should look something like this:

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()

        self.addEmployee(master)
jath03
  • 2,029
  • 2
  • 14
  • 20
  • When i add `self.master` recursion happens – hiihihihelloo Jul 18 '18 at 23:24
  • 1
    @hiihihihelloo: The recursion happens because `MainPageGUI` creates a `Database` instance, but `Database` also creates a new `MainPageGUI`. You need to decide which direction that should work in, and get rid of the creation going in the other direction. If both objects need a reference to the other, have one pass a reference to itself to the other one, rather than a reference to `master`. – Blckknght Jul 18 '18 at 23:42