how to split a program into more than one file with threads?

Greetings all,

I am having trouble understanding how to handle a GUI in my situation.

The example of how to use threads in the documentation that i am following is here.

http://wiki.wxpython.org/LongRunningTasks

What I am after is how to put the WorkerThread into it’s own file and use from worker_thread import WorkerThread in the MainFrame file.

i have been successful so far, but where i get hung up is what to do with the EVT_RESULT_ID .

If i have both in the same file, it works, but if i declare EVT_RESULT_ID in both files, the thread runs, but does not update the GUI.

I appreciate any help here. Still very much a beginner to wxpython.

The example of how to use threads in the documentation that i am following
is here.
LongRunningTasks - wxPyWiki

What I am after is how to put the WorkerThread into it's own file and use
from worker_thread import WorkerThread in the MainFrame file.
i have been successful so far, but where i get hung up is what to do with
the EVT_RESULT_ID .
If i have both in the same file, it works, but if i declare EVT_RESULT_ID in
both files, the thread runs, but does not update the GUI.

here is the line:
# Define notification event for thread completion

EVT_RESULT_ID = wx.NewId()

what that says is: "give me a unique ID to use".

so if you have that same line in two different modules, you'll get two
different IDs -- which is NOT what you want.

I'd put that line in the thread module, then get it from the thread
module in your other module:

EVT_RESULT_ID - thread_module.EVT_RESULT_ID

then they will be the same.

but yo may not need that ID --

that code defines a new event:

def EVT_RESULT(win, func):
  ....

so what you really need is that: EVT_RESULT. So you can change this line:

     # Set up event handler for any worker thread results
         EVT_RESULT(self,self.OnResult)
to:

     # Set up event handler for any worker thread results
         thread_module.EVT_RESULT(self,self.OnResult)

and that should do it (if the event definitions are in teh thread
module, which they should be.

NOTE: you can make all this easier by using wx.CallAfter instead.

HTH,
    -chris

···

On Tue, Apr 3, 2012 at 8:41 AM, Shawn Bright <shawn@skrite.net> wrote:

I appreciate any help here. Still very much a beginner to wxpython.

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

i am intrigued by the suggestion to use wx.CallAfter().

I did some looking, but can’t see how i would implement it for what i am trying to do here.

thanks for the tips…

···

On Tuesday, April 3, 2012 10:41:01 AM UTC-5, Shawn Bright wrote:

Greetings all,
I am having trouble understanding how to handle a GUI in my situation.

The example of how to use threads in the documentation that i am following is here.

http://wiki.wxpython.org/LongRunningTasks

What I am after is how to put the WorkerThread into it’s own file and use from worker_thread import WorkerThread in the MainFrame file.

i have been successful so far, but where i get hung up is what to do with the EVT_RESULT_ID .

If i have both in the same file, it works, but if i declare EVT_RESULT_ID in both files, the thread runs, but does not update the GUI.

I appreciate any help here. Still very much a beginner to wxpython.

what are you trying to do?

anyway, you can pass extra arguments to wx.CallAfter(), so in the
example you're looking at, you would change:

def OnResult(self, event):

to something like:

    def NewResult(self, data):
        """Show Result status."""
        if data is None:
            # Thread aborted (using our convention of None return)
            self.status.SetLabel('Computation aborted')
        else:
            # Process results here
            self.status.SetLabel('Computation Result: %s' % event.data)
        # In either event, the worker is done
        self.worker = None

then to call it in your thread:

wx.CallAfter(TheFrame.NewResult, data)

assuming you have a reference to the TheFrame in the module.

you should be abel to find Callafter() examplesin teh Wiki.

-Chris

···

On Tue, Apr 3, 2012 at 10:29 AM, Shawn Bright <shawn@skrite.net> wrote:

i am intrigued by the suggestion to use wx.CallAfter().
I did some looking, but can't see how i would implement it for what i am
trying to do here.

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

OK, better and cleaner examples.

In these attached examples, you can run and see that the second worker thread is running because it is printing to the terminal, however, nothing is making it to the MainFrame

Please have a look, and let me know how to proceed. Thanks.

rf_thread.py (1.22 KB)

pivotrac_control_panel.py (3.67 KB)

···

On Tuesday, April 3, 2012 10:41:01 AM UTC-5, Shawn Bright wrote:

Greetings all,
I am having trouble understanding how to handle a GUI in my situation.

The example of how to use threads in the documentation that i am following is here.

http://wiki.wxpython.org/LongRunningTasks

What I am after is how to put the WorkerThread into it’s own file and use from worker_thread import WorkerThread in the MainFrame file.

i have been successful so far, but where i get hung up is what to do with the EVT_RESULT_ID .

If i have both in the same file, it works, but if i declare EVT_RESULT_ID in both files, the thread runs, but does not update the GUI.

I appreciate any help here. Still very much a beginner to wxpython.

OK, better and cleaner examples.
In these attached examples, you can run and see that the second worker
thread is running because it is printing to the terminal, however, nothing
is making it to the MainFrame
Please have a look, and let me know how to proceed. Thanks.

you've defined RF_RESULT_ID twice.

The one in pivotrac_control_panel and rf_thread are two different
values, so not the same, and EVT_RESULT are not the same either -- you
jsut want those defined in one place.

I've moved all the definitions of the events and the threads to the
other file, and imported those names. I also made two completely
separate event classes (and did a bit of other cleanup). It really
doesn't matter where you define things, as long as you refer to the
same one when you use them -- but putting the event definitions in the
same module as the threads made it easier to avoid circular imports.

The enclosed sample seems to work.

-Chris

pivotrac_control_panel.py (2.44 KB)

rf_thread.py (2.31 KB)

···

On Tue, Apr 3, 2012 at 1:40 PM, Shawn Bright <shawn@skrite.net> wrote:

On Tuesday, April 3, 2012 10:41:01 AM UTC-5, Shawn Bright wrote:

Greetings all,
I am having trouble understanding how to handle a GUI in my situation.
The example of how to use threads in the documentation that i am following
is here.
LongRunningTasks - wxPyWiki

What I am after is how to put the WorkerThread into it's own file and use
from worker_thread import WorkerThread in the MainFrame file.
i have been successful so far, but where i get hung up is what to do with
the EVT_RESULT_ID .
If i have both in the same file, it works, but if i declare EVT_RESULT_ID
in both files, the thread runs, but does not update the GUI.

I appreciate any help here. Still very much a beginner to wxpython.

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

OK,

Here is a version with wx.CallAfter instead -- seems easier to me.

I also noticed that you didn't need two event types before, as you
were passing the info about where it came from in the data...

While you are at it -- do take a look at the Style Guide -- and take
the time to learn sizers -- it's worth it.

HTH,
   -Chris

rf_thread.py (1.62 KB)

pivotrac_control_panel.py (2.23 KB)

···

On Tue, Apr 3, 2012 at 10:10 PM, Chris Barker <chris.barker@noaa.gov> wrote:

On Tue, Apr 3, 2012 at 1:40 PM, Shawn Bright <shawn@skrite.net> wrote:

OK, better and cleaner examples.
In these attached examples, you can run and see that the second worker
thread is running because it is printing to the terminal, however, nothing
is making it to the MainFrame
Please have a look, and let me know how to proceed. Thanks.

you've defined RF_RESULT_ID twice.

The one in pivotrac_control_panel and rf_thread are two different
values, so not the same, and EVT_RESULT are not the same either -- you
jsut want those defined in one place.

I've moved all the definitions of the events and the threads to the
other file, and imported those names. I also made two completely
separate event classes (and did a bit of other cleanup). It really
doesn't matter where you define things, as long as you refer to the
same one when you use them -- but putting the event definitions in the
same module as the threads made it easier to avoid circular imports.

The enclosed sample seems to work.

-Chris

On Tuesday, April 3, 2012 10:41:01 AM UTC-5, Shawn Bright wrote:

Greetings all,
I am having trouble understanding how to handle a GUI in my situation.
The example of how to use threads in the documentation that i am following
is here.
LongRunningTasks - wxPyWiki

What I am after is how to put the WorkerThread into it's own file and use
from worker_thread import WorkerThread in the MainFrame file.
i have been successful so far, but where i get hung up is what to do with
the EVT_RESULT_ID .
If i have both in the same file, it works, but if i declare EVT_RESULT_ID
in both files, the thread runs, but does not update the GUI.

I appreciate any help here. Still very much a beginner to wxpython.

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Brilliant ! Thanks so much.

This looks like it has all of the pieces that i need to incorporate into the actions i am needing to do.

I will post here if i run into further trouble.

Thanks again, i have learned a lot.

sk

···

On Tuesday, April 3, 2012 10:41:01 AM UTC-5, Shawn Bright wrote:

Greetings all,
I am having trouble understanding how to handle a GUI in my situation.

The example of how to use threads in the documentation that i am following is here.

http://wiki.wxpython.org/LongRunningTasks

What I am after is how to put the WorkerThread into it’s own file and use from worker_thread import WorkerThread in the MainFrame file.

i have been successful so far, but where i get hung up is what to do with the EVT_RESULT_ID .

If i have both in the same file, it works, but if i declare EVT_RESULT_ID in both files, the thread runs, but does not update the GUI.

I appreciate any help here. Still very much a beginner to wxpython.

OK, i ran into another problem with creating a new Class and running from a different thread.

when i call the new result event, i am given an argument about the number of arguments i am passing.

i have attached the new files.

thanks

other_worker.py (1.15 KB)

pivotrac_control_panel.py (3 KB)

rf_thread.py (2.31 KB)

···

On Tuesday, April 3, 2012 10:41:01 AM UTC-5, Shawn Bright wrote:

Greetings all,
I am having trouble understanding how to handle a GUI in my situation.

The example of how to use threads in the documentation that i am following is here.

http://wiki.wxpython.org/LongRunningTasks

What I am after is how to put the WorkerThread into it’s own file and use from worker_thread import WorkerThread in the MainFrame file.

i have been successful so far, but where i get hung up is what to do with the EVT_RESULT_ID .

If i have both in the same file, it works, but if i declare EVT_RESULT_ID in both files, the thread runs, but does not update the GUI.

I appreciate any help here. Still very much a beginner to wxpython.

http://wiki.wxpython.org/DoNotTopPost

     return _core_.PyApp__BootstrapApp(*args, **kwargs)
   File "pivotrac_control_panel.py", line 84, in OnInit
     self.frame = MainFrame(None, -1)
   File "pivotrac_control_panel.py", line 39, in __init__
     OW_ResultEvent(self, self.OnResult)
TypeError: __init__() takes exactly 2 arguments (3 given)

You probably intended to call EVT_OW_RESULT there to do the event binding instead of creating an instance of the OW_ResultEvent class.

···

On 4/4/12 8:08 AM, Shawn Bright wrote:

OK, i ran into another problem with creating a new Class and running
from a different thread.
when i call the new result event, i am given an argument about the
number of arguments i am passing.
i have attached the new files.

--
Robin Dunn
Software Craftsman

Indeed that was the problem. Thanks very much.

about the top post thing… i think i am getting confused about how to avoid that.

Will look into it though, i don’t want to contribute clutter.

thanks again for the help

skrite

···

On Wed, Apr 4, 2012 at 11:28 AM, Robin Dunn robin@alldunn.com wrote:

On 4/4/12 8:08 AM, Shawn Bright wrote:

OK, i ran into another problem with creating a new Class and running

from a different thread.

when i call the new result event, i am given an argument about the

number of arguments i am passing.

i have attached the new files.

http://wiki.wxpython.org/DoNotTopPost

return _core_.PyApp__BootstrapApp(*args, **kwargs)

File “pivotrac_control_panel.py”, line 84, in OnInit

self.frame = MainFrame(None, -1)

File “pivotrac_control_panel.py”, line 39, in init

OW_ResultEvent(self, self.OnResult)

TypeError: init() takes exactly 2 arguments (3 given)

You probably intended to call EVT_OW_RESULT there to do the event binding instead of creating an instance of the OW_ResultEvent class.

Robin Dunn

Software Craftsman

http://wxPython.org

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

about the top post thing… i think i am getting confused about how to avoid that.
Will look into it though, i don’t want to contribute clutter.

Just always put your words under the words of the other person’s. There’s nothing further to look into. Use your arrow keys/mouse, whatever, but move your cursor there and type.

(Also, please trim unnecessary words from the discussion, too. Just aim to make the whole thing as readable and have it “flow” as much as possible).

Che

about the top post thing.. i think i am getting confused
about how to avoid that.
Will look into it though, i don't want to contribute clutter.

Just always put your words under the words of the other
person's. There's nothing further to look into. Use your
arrow keys/mouse, whatever, but move your cursor there and type.

It's easier with some e-mail clients than others (*cough* Outlook *cough*)
and is easier with text e-mails than HTML e-mails in some e-mail clients. I
wish there was never anything further to look into. Just sayin'.

David

about the top post thing..

...

Just always put your words under the words of the other person's.

...

(Also, please trim unnecessary words from the discussion, too. Just aim to
make the whole thing as readable and have it "flow" as much as possible).

+sys.maxint

I know the fashion now is to bottom post, but frankly, blind bottom
posting is worse than blind top posting -- I HATE having to scroll
down through a huge thread of stuff to find the one new line at the
bottom.

Most of us are generally reading a thread, not an email late in the
thread by itself.

So the best option is what Che suggests -- trim the message, so that
your new stuff directly relates to what's there.

Just sayin'

-Chris

···

On Wed, Apr 4, 2012 at 10:50 AM, C M <cmpython@gmail.com> wrote:

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Agreed. When I say don't top-post I don't mean to fully bottom-post, but rather to intersperse your comments in the original message's text where they are most relevant, and to trim anything that is not relevant.

···

On 4/4/12 11:29 AM, Chris Barker wrote:

On Wed, Apr 4, 2012 at 10:50 AM, C M<cmpython@gmail.com> wrote:

(Also, please trim unnecessary words from the discussion, too. Just aim to
make the whole thing as readable and have it "flow" as much as possible).

+sys.maxint

I know the fashion now is to bottom post, but frankly, blind bottom
posting is worse than blind top posting -- I HATE having to scroll
down through a huge thread of stuff to find the one new line at the
bottom.

--
Robin Dunn
Software Craftsman