[Tutor] How to access a method defined in one class from another class (which is a thread) in Python3?

Prasad, Ramit ramit.prasad at jpmorgan.com.dmarc.invalid
Thu Aug 8 19:17:18 CEST 2013


SM wrote:
> 
> I am defining multiple classes (some of them are threads)
> I have been writing Python programs for a few months, but never faced this issue so far unless I am
> doing something different inadvertently. The only difference I see is that I am calling the methods
> belonging to other classes, from a class which is also a thread.
>  I see the following error:
> 
> AttributeError: 'Ui_MainWindow' object has no attribute 'textEdit_fwcmdlineoutput'
> Code Snippets:
> 
> class Ui_MainWindow(object):
>     [snip]
> 
>     def setStdoutToTextEditWindowFw(self):
>           self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() )
>           sys.stdout = self.oldstdout
> Calling the above method from within the class works fine. But I am calling it from another class as
> below:
> 
> class bcThread(threading.Thread):
>     def __init__(self, cmd):
>         threading.Thread.__init__(self)
>         self.cmd = cmd
>     def run(self):
>         [snip]
>         Ui_MainWindow.setStdoutToTextEditWindowFw(Ui_MainWindow)
> The highlighted line gives the following error :
> 
> Exception in thread Thread-1:
> Traceback (most recent call last):
>   File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner
>     self.run()
>   File "bc_reports_tab.py", line 1299, in run
>     Ui_MainWindow.setStdoutToTextEditWindowFw(Ui_MainWindow)
>   File "bc_reports_tab.py", line 465, in setStdoutToTextEditWindowFw
>     self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() )
> AttributeError: type object 'Ui_MainWindow' has no attribute 'textEdit_fwcmdlineoutput'
> I also tried many different ways of calling the method. The highlighted line is one of them. Another
> one I tried is here where I create an instance, which also gives the same error:
> 
> x = Ui_MainWindow()
> x.setStdoutToTextEditWindowFw()
> I see the same error.

I do not think that actually gave you the same error.
Most likely it gave you a *similar* error. See the below.

>>> o = object()
>>> o.blah # on instance object, not class object
AttributeError: 'object' object has no attribute 'blah'

>>> object.blah # on class object, not instance object
AttributeError: type object 'object' has no attribute 'blah'


If you got the top error from the snippet where you instantiate 
an instance of Ui_MainWindow then I believe some function must 
create the textEdit_fwcmdlineoutput attribute and that needs 
to be called first. I would have imagined this creation should 
occur on __init__ but obviously it must be somewhere else. 
You will need to call that section of code. It should be in 
the Ui_MainWindow class and look something like:

self.textEdit_fwcmdlineoutput = <create TextEdit object here>

> Can someone guide me as to what is the correct way to do something like this?
> 
> Thanks in advance.
> -SM
> 



This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list