[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:24:41 CEST 2013


Hi Alan,
Thanks for your quick and detailed reply.
I guess what you are suggesting is very similar to what I am doing (of
course not when I use the class. I tried using the class when I couldn't
figure out why it gave error when I used the object).  Looking at your
example, I ended up doing exactly what you suggested. I am sure I am
missing something, as it is giving the same error:
Here is what I am doing based on your suggestion:

class bcThread(threading.Thread):
    def __init__(self, cmd):
        threading.Thread.__init__(self)
        self.cmd = cmd


    def h(self, y):
        y.setStdoutToTextEditWindowFw()   #(line 1277)

    def run(self):
        # Run the command first.
        (data, err) = Popen(self.cmd, stdout=PIPE,
stderr=PIPE).communicate()
         [snip]
         x = Ui_MainWindow()
         self.h(x)    # (line 1319)


class Ui_MainWindow(object)
    def setupUi(self, MainWindow):
        [snip]

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

self.textEdit_fwcmdlineoutput.setObjectName(_fromUtf8("textEdit_fwcmdlineoutput"))

    [snip]
    (line: 475)
    def setStdoutToTextEditWindowFw(self):
        self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() )
        sys.stdout = self.oldstdout


   The error looks like this:

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 1319, in run
    self.h(x)
  File "bc_reports_tab.py", line 1277, in h
    y.setStdoutToTextEditWindowFw()
  File "bc_reports_tab.py", line 475, in setStdoutToTextEditWindowFw
    self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() )
AttributeError: 'Ui_MainWindow' object has no attribute
'textEdit_fwcmdlineoutput'


Wonder why it says the class/object has no attribute.

Thanks,
-SM


On Thu, Aug 8, 2013 at 1:22 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> On 08/08/13 17:23, 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.
>>
>
> Your main issue is that you are trying to program using classes not
> objects.
> The normal usage of classes is to create an object from the class and then
> call the methods via the objects. You can pass objects around as arguments
> to the methods etc.
>
> So if you have two classes A and B you can create objects
> from those called, say, a and b:
>
> class A:
>    def f(self):
>       print 'in A.f'
>
> class B:
>    def g(self):
>       print 'in B.g'
>    def h(self,anObject)
>       anObject.f()   #cxall method of the object passed in
>
> #create the object instances
> a = A()
> b = B()
> a2 = A()    # a second object of class A.
>
>
> #Now we can call the methods using the objects
> a.f()   #-> print "in A.f"
> b.g()   #-> print "in B.g"
> a2.f()  #-> print "in A.f" again
>
> # pass object a into object B's h() method
> b.h(a)  # use a from b to print "in A.f"
> b.h(a2) # and again using a2 this time
>
> So by creating objects within your threads you
> can access the methods of your classes. You can even access
> global level objects from multiple threads and share state
>  - but be careful about synchronising and locking issues.
>
> Finally you need to make sure your classes are visibnle in the threads
> that use them. So if you define a master class per thread and keep each in
> its own module then each module will need to import the other class modules
> before it can use them.
>
> I'm going out soon and typed that in a hurry. I hope it makes sense and
> doesn't have too many mistakes(all untested!).
>
> HTH
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130808/bd0d7641/attachment-0001.html>


More information about the Tutor mailing list