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

SM sunithanc at gmail.com
Thu Aug 8 22:03:21 CEST 2013


Ramit:
Thanks for the quick response. You are right about the error. When I did
the following:
x = Ui_MainWindow()
x.setStdoutToTextEditWindowFw()

I got the following error:
AttributeError: 'Ui_MainWindow' object has no attribute
'textEdit_fwcmdlineoutput'

But I do have code that creates an attribute in Ui_MainWindow() class:

self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw)

This is what is making me get confused as to why it complains that there is
no attribute.

Thanks,
-SM



On Thu, Aug 8, 2013 at 1:17 PM, Prasad, Ramit <ramit.prasad at jpmorgan.com>wrote:

> 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130808/761a16f6/attachment.html>


More information about the Tutor mailing list