0

Suppose I have 2 files, frontend.py for displaying a GUI and backend.py for database functions. I am having an issue inheriting the database class using super(). from #backend2.py in the managerPage class in #frontend.py

I have the error:

frame = F(container, self) TypeError: init() missing 2 required positional arguments: 'cn' and 'cur'

I believe this has to do with init in the tk.Frame class which i have also inherited in the managerPage class. I believe the selfService class (which I have renamed for my own sake) is credited to user @BryanOakley. Could someone please help me successfully utilise super().

frontend.py.

import backend2

class selfService(tk.Tk, Toplevel):

    def __init__(self, *args, **kwargs):
        #Toplevel.__init__(self)
        self.employeeWindow = None


        tk.Tk.__init__(self, *args, **kwargs)
        self.container = tk.Frame(self)

        self.container.pack(side="top", fill="both", 
        expand = True)

        self.container.grid_rowconfigure(0, 
        weight=1)
        self.container.grid_columnconfigure(0, 
        weight=1)

        self.frames = {}


        selfService.restart = False

        for F in (StartPage, PageOne, PageTwo):

        self.frame = F(self.container, self)

        self.frames[F] = self.frame

        self.frame.grid(row=0, column=0, 
        sticky="nsew")

        self.show_frame(StartPage)


    def show_frame(self, cont):
        self.frame = self.frames[cont]
        self.frame.tkraise()

class managerPage(tk.Frame, 
    validation.account_validation,backend2.Database):
    def __init__(self, parent, controller, cn, cur):
        tk.Frame.__init__(self,parent)
        super().__init__(cn, cur)

        print(self.cn)

backend2.py

class Database():
    def __init__(self):
        self.config = {
        'user': 'root',
        'password': 'root',
        'host': 'localhost',
        'database': 'myDatabase'}

        self.cn = 
        mysql.connector.connect(**self.config)
        self.cur = self.cn.cursor()
1
  • 1
    You're using inheritance in a way that suggests you shouldn't be using inheritance. Are you certain that you want each page to have its own separate database connection? Ie: if you have 3 pages, you'll have 3 distinct connections to the database. That's an unusual design. Commented Jan 29, 2020 at 19:16

1 Answer 1

0

Inheritance isn't for sharing data between two different objects. Your managerPage should not inherit your Database class. The page can't both be a page and a database at the same time. Each instance would be a separate instance of the database if you used inheritance.

Instead, you should create a single instance of the Database class, and pass that instance to the pages that need it. That, or add a method on the controller which can return the instance.

For example, you can create the database instance in the __init__ of your controller:

class selfService(tk.Tk):

    def __init__(self, *args, **kwargs):
        ...
        self.db = Database()
        ...

Then, in any code that has a reference to the controller you can access the database with self.controller.db, like in the following example:

class managerPage(tk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ...
        print(self.controller.db)

(as a side note, you shouldn't be inheriting from Tk and Toplevel. One or the other, but not both).

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. Thanks. But when instantiating the db class in selfService, I am subject to the error: return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'db'
@grimReaperZ: like My first example shows, you have to define self.db.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.