Using external class methods inside the imported module
<p>My python application consists of various <em>separate</em> processing algorithms/modules combined within a single (Py)Qt GUI for ease of access. </p>

<p>Every processing algorithm sits within its own module and all the communication with GUI elements is implemented within a single class in the main module. 
In particular, this GUI class has a progressbar (<a href="http://pyqt.sourceforge.net/Docs/PyQt4/qprogressbar.html" rel="nofollow">QProgressBar</a>) object designed to represent the current processing progress of a chosen algorithm.
This object has <code>.setValue()</code> method (<code>self.dlg.progressBar.setValue(int)</code>).</p>

<p>The problem is that since <code>self.dlg.progressBar.setValue()</code> is a class method I cannot use it inside my imported processing modules <em>to report their progress state within their own code</em>. </p>

<p>The only workaround I found is to add <code>progressbar</code> variable to definition of each processing module, pass there <code>self.dlg.progressBar</code> inside the main module and then blindly call <code>progressbar.setValue(%some_processing_var%)</code> inside the processing module. </p>

<p>Is this the only way to use outer class methods inside imported modules or are there better ways? </p>

<p>No. I think this approach somewhat breaks software engineering principles (e.g. <a href="https://en.wikipedia.org/wiki/Single_responsibility_principle" rel="nofollow">single responsibility</a>). In single responsibility principle, each module is only in charge of its assigned task and nothing else. If we consider UI a separate layer, so your processing modules shouldn't have anything to do with the UI layer.</p>

<p>In this case, your modules should have a method called <code>publish_progress(callback)</code> which <code>callback</code> is a function to be called for each progress step (<a href="https://en.wikipedia.org/wiki/Callback_(computer_programming)" rel="nofollow">more info</a>). Then, in your UI layer define a function which is given an integer (between 0 to 100) and updates the progress bar. Once you've defined it, you should register it with <code>publish_progress</code> method of your modules.</p>

<pre><code>def progress_callback(prg):
    self.dlg.progressBar.setValue(prg)
</code></pre>

<p>Registering it:</p>

<pre><code>my_module.publish_progress(progress_callback)
</code></pre>

<p>Calling the callback in your module:</p>

<pre><code>progress_callback(0)
...
# do something
...
progress_callback(20)
...
# do something
...
progress_callback(100)
</code></pre>

python qt class module pyqt