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

SM sunithanc at gmail.com
Fri Aug 9 03:52:58 CEST 2013


>>I'm confused. Why does the OP want multiple instances of the user
interface widgets?

> He may not, but he is creating them! But it is possible he does want
multiple duplicate windows, one monitoring each thread. (eg progress
dialogs on an FTP program). Since I don't know the context I'm
assuming he knows how many windows he wants!

If he does want just one then he needs to reference the global object in
his run() method... But that brings us back into the land of
synchronisation issues and threadsafe locks etc.

OP is me? Not sure what it stands for, but I am a 'she' :)
Since you asked here is some context on what I am trying to do:
I don't want multiple instances of the widget. I have a Gui with 3 tabs,
each for a different purpose, with a unique functionality within the main
window.  Each tab has only one instance of this widget I am asking question
about. Everything is working great with full functionality. As an added
feature, I am now asked to have a "progress bar" widget which appears when
an operation is going on, to tell the user that he/she has to wait. So I am
running two threads - first one is the progressbar widget which has a
rectangular slab spinning sideways to indicate that the operation is going
on. The second thread is the one which is actually doing the operation . I
have a flag that the second thread sets after the job is done, which the
widget thread keeps checking in a loop and stops spinning and gets out of
the loop when the flag is set.  I have that working as well.
I also have a textEdit window on the Gui where I redirect all the standard
output using StringIO. For that I use the following code:
Before the operation starts, I call the following:
    self.oldstdout = sys.stdout
    sys.stdout = StringIO()

After the operation, I call the method:
  def setStdoutToTextEditWindowFw(self):
        self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() )
        sys.stdout = self.oldstdout
I need to call it from the thread after the job is done, so the text I want
to output on the command line will show up on the textEdit window on the
Gui.

The above method is defined on Ui_MainWindow class. The widget gets created
in the method setupUI which is defined under Ui_MainWindow class:
self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw)

Alan indicated that it should be called by __init__.
I tried doing it and the gui crashed!  (I am yet to see why and where it
crashed)
I am not sure I know what the problem or the solution is. Hence the email
Thanks for all your time, suggestions and help.

Thanks,
-SM



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

> On 08/08/13 23:02, eryksun wrote:
>
>  You are creating a new instance inside your run() method but
>>> the call in main() only sets it for the instance created in
>>> main. It won't set it up for any other instance. You need
>>> to call it every time you create a new object. Unless....
>>>
>>> If you put it in init() it will be called every time you
>>> create a new instance. Much safer.
>>>
>>
>> I'm confused. Why does the OP want multiple instances of the user
>> interface widgets?
>>
>
>
> He may not, but he is creating them! But it is possible he does want
> multiple duplicate windows, one monitoring each thread. (eg progress
> dialogs on an FTP program). Since I don't know the context I'm
> assuming he knows how many windows he wants!
>
> If he does want just one then he needs to reference the global object in
> his run() method... But that brings us back into the land of
> synchronisation issues and threadsafe locks etc.
>
>
> --
> 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/a1bd1253/attachment.html>


More information about the Tutor mailing list