[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 17:50:19 CEST 2013


On Fri, Aug 9, 2013 at 4:01 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> On 09/08/13 02:52, SM wrote:
>
>  OP is me? Not sure what it stands for, but I am a 'she' :)
>>
>
> Oops. my bad. Stereotyping is a bad habit.

[SM]: Not your fault. My bad too. Dog's face tells little about the
owner...

>
>
>  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
>>
>
> So you do want multiple instances of the widget. You want a separate
> instance in each of the 3 tabs. But you only want one main UI.
>

No, no! Each tab is totally distinct from the other. I shouldn't have
mentioned the tab. Each tab has its own  widget
(ex: self.tab_fw = QtGui.QWidget(), self.tab_ann = QtGui.QWidget(), etc),
its own textEdit window and its own progress bar widget.
All the tabs are defined within this single class - they are not instances
of the class, as the tabs are distinct from each other. All of that is
working really well.


> So you should probably create a separate class for the widgets that are
> duplicated and then insert the instances of that class into your main UI
> object. The new widget class should have the methods you want to access
> from your threads.
>
>
>  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.
>>
>
> Is this progress bar duplicated in each tab or is that another thing
> entirely? If it is yet another widget set you may want to make that a
> separate class too.

[SM]: Yes. I shouldn't say duplicated, but each tab has a different
progress bar of its own. In the example, I was quoting the code for just on
tab. The others will be implemented similarly.

>
>
>  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 wouldn't normal use a thread for the progress bar I'd just use a timer
> event in my main GUI to check the flag periodically. Threads will work but
> they are more effort to setup and manage.


[SM]: I did start out with a timer. But due to my lack of experience with
usage of timer in Python, the code I wrote was running sequentially - that
is, the two jobs I am trying to synchronize - the progress bar and the job
to be done - were running one after the other, beating the whole purpose.
So I had to go for the thread-based implementation.
If you or anyone else reading this have a sample code for  a timer-based
implementation, I could try that. I did use an example I found online, but
that didn't help.

>
>
>  I also have a textEdit window on the Gui where I redirect all the
>> standard output using StringIO.
>>
>
> Just one text window in the main GUI? not one per tab or per thread?

[SM]. One text window oer tab - not per thread. I am using threads only for
running the progress bar.



>
>  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__.
>>
>
> Only really needed if you want to create a new widget per thread,
> it sounds like you only want this in the main UI so main() is OK (although
> putting it in init shouldn't be a problem!)
>
>
>  I tried doing it and the gui crashed!
>>
>
> I can only think that maybe you are overwriting some global values that
> were set in the call in the first widget. The first objective should be to
> clearly work out how many windows you are creating and where. It sounds
> like you have a main window class, 3 tab classes and maybe progress
> indicator and text window classes too.
>
> Then build up the GUI structure by adding instances of the tabs
> to the main window instance and adding the progress indicator instance(s)
> when you start the processing threads. Try to map the visible objects in
> your GUI to objects(classes) in your code.
>
> Having said that I've never used Qt so there may be ready made
> classes in the toolkit (tabs etc) that you can use out of the box.
> In fact progress indicator dialogs are quite a common toolkit
> feature so that may well exist already too.,

[SM] I think my answers on the top will clarify these.

Thanks for all your time! Really appreciate it.

-SM

>
>
> --
> 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/20130809/ead86eda/attachment.html>


More information about the Tutor mailing list