How could I make a good application architecture in Python with PyQt?

1.6k Views Asked by At

I think my appication doesn't have a good architecture. I can't figure out how to navigate between files correctly.

Here is a non-standardized schema of my application.

enter image description here

Each file contains one class.

The principal problem is that I can't get some variables from any class to another because of the architecture of the code. If I put all my classes in one file, it will certainly runs but I want to separate all my classes (which are QWindowand QWidget) in several files.

1

There are 1 best solutions below

0
On BEST ANSWER

1. In every parent class, import each child with :

from childfilename import childclassname

For example, in the optionwindow class, you will have :

from mainwindowfilename import mainwindowclassname
from toolbarfilename import toolbarsclassname
from menubarfilename import menubarclassname

Note 1 : For better lisibility, name your files with the same name as your classes. *for example : mytoolbar.py contains class mytoolbar(QtGui.QToolBar)*

Note 2 : You need to place an empty __init__.py in every folder that contains a class)

Note 3 : You can place classes in different folders. If you do so, use :

from folder.childfilename import childclassname

2. To instantiate and call child functions or get child variables :

In the parent class :

self.child1=childclassname()
self.child1.childfunction1()
a1 = self.child1.childvariable1

In the child class :

...
self.childvariable1 = 2
def childfunction1(self):
    ...

3. To get parent or grand-parents variables or functions in child : In the parent class :

...
self.parentvariable1 = 2
def parentfunction1(self):
    ...

In the child class :

self.parent().parentfunction1()`
a1 = self.parent().parentvariable1`

Note : self.parent().parent(). ... will get you to the grand parent.

I hope it helped