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

Alan Gauld alan.gauld at btinternet.com
Thu Aug 8 22:57:07 CEST 2013


On 08/08/13 21:24, SM wrote:

> 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 h(self, y):
>          y.setStdoutToTextEditWindowFw()   #(line 1277)

...
>           [snip]
>           x = Ui_MainWindow()
>           self.h(x)    # (line 1319)

So far so good.

> class Ui_MainWindow(object)
>      def setupUi(self, MainWindow):
>          [snip]
>
>          self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw)

This only comes into existence after setupUi is called.
Normally that would be in the UI init() method but you don;t
show any init or any other call to setupUi

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

>      def setStdoutToTextEditWindowFw(self):
>          self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() )

So this fails if setupUi has not been called earlier because the 
attribute has not been initialised yet.

>      self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() )
> AttributeError: 'Ui_MainWindow' object has no attribute
> 'textEdit_fwcmdlineoutput'

You need to ensure that the UI __init__() method calls self.setupUi()
I suspect.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list