[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
Sun Aug 11 18:05:30 CEST 2013


On 09/08/13 16:50, SM wrote:

Sorry I only just picked this up.

> (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.

OK, I'll assume the structure looks like

MainGUIWindow
- Tab1
   - TextEdit1
   - ProgressBar1
   - any other widgets on tab1
- Tab2
   - TextEdit2
   - ProgressBar2
   - any other widgets on tab2		
- Tab3
   - TextEdit3
   - ProgressBar3
   - any other widgets on tab3		

And that both the TextEdit and progress bar widgets are part of
the Qt framework/library? So there are 3 distinct progress bar 
instances, one per tab?

>         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 an extra (4th?) progress bar or is this the ones already 
described as being in the tabs?

Also, when you say "appears" do you mean its invisible until the 
operation starts? Or is it visible but inactive (maybe greyed out?)

> [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.

OK So I'm again going to assume:

A total of 1 progress bar per tab. The "added feature" is what these 
bars are addressing?

>         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.

I usually expect a progress bar to indicate the percentage complete but 
this sounds like its just a spinning eggshell activity indicator. Is 
that correct? You have no progress value indication?

> 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

OK, again I'll assume we are dealing with just an activity indicator not 
a value display.

>         and stops spinning and gets out of the loop when the flag is set.  I
>         have that working as well.

OK, Good the picture is building.

> [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 sounds like you tried to use a Python native timer. Normally in a 
GUI the toolkit (Qt in this case) provides a timer event that triggers
via the normal event mechanism. You set the timer, the event arrives and 
you do something then reset the timer for the next time round.

I found this page that describes the Qt approach (which includes
multi-shot timers) and links to more detailed examples etc.

http://qt-project.org/doc/qt-4.7/timers.html#id-fd46b213-376e-4636-a7d2-8ae899de1e11

The code is in C++ but should translate easily to python and PyQt.

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



More information about the Tutor mailing list