Passing variables from normal function to init_UI function
<p>I have been facing a problem in using a variable of a normal class function and <code>initUI</code> function.<br>
The code looks like this:</p>

<pre><code>def text_trans(self):
    ...
    number = 2
    ...
def init_UI(self):
    number = text_transfer(self)
    print(number)
    QtWidgets.QMessageBox.information(self,"generated","generated")
</code></pre>

<p>I wrote the above code but it shows a name error. Is there a way, I can use the number variable in the first function in the second one?</p>

<blockquote>
  <p>I wrote the above code but it shows a name error.</p>
</blockquote>

<p>Probably because, for starters, <code>text_transfer != text_trans</code> and, secondly, in the body of methods you should use <code>self</code> to access other methods. So, <code>init_UI</code> should look like:</p>

<pre><code>def init_UI(self):
    number = self.text_trans()
    print(number)
</code></pre>

<p>You access the name through <code>self</code>, and <code>self</code> is then implicitly passed to the invoked function, so <code>text_transfer(self)</code> won't work.</p>

<blockquote>
  <p>Is there a way, I can use the number variable in the first function in the second one?</p>
</blockquote>

<p>In most cases, this is done by simply <em>returning</em> the wanted name from a given function:</p>

<pre><code>def text_trans(self):
    ...
    number = 2
    return number
</code></pre>

python function python-3.x pyqt5