[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
Fri Aug 9 10:01:04 CEST 2013


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.

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

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.

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

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

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

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



More information about the Tutor mailing list