| | |
Can't update wxPython UI during subprocess
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 7
Reputation:
Solved Threads: 0
I'm making a simple wxPython app on win32 that does a number of calls to external binaries. Everything works fine, except the GUI doesn't update well while the external process is running. This makes for an ugly window and non-updated status StaticTexts, so I'd like to fix it. Window . Update () helps some, but it still doesn't do the job. I've also tried Window . Refresh () and Window . UpdateWindowUI () and every combination of the three, but no luck. Any thoughts?
Python Syntax (Toggle Plain Text)
# Call process Process = subprocess . Popen ( Command_Line ) # Wait on process to complete while Process . poll () == None : # Update GUI Window . Update () # Wait a bit, don't hog the CPU time . sleep ( 0.25 )
You'll need to use threading to handle this. One thread calls the function which does the subprocess, and the other calls a function which updates the GUI. Something along the lines of:
It's started running the myFunc function's endless loop and func2's stuff while it can continue on with other things in the script. Check this for more info:
http://docs.python.org/library/thread.html
Also, please don't use such bad writing style in your code. Only put spaces between non-unary operators. Or, at least don't put spaces between the periods denoting namespaces (like "time . sleep(0.25)") and the colons in your conditional statements. You can check here for a style guide for Python.
python Syntax (Toggle Plain Text)
# untested! import thread from time import sleep def myFunc(): while True: print 'Hello world!' sleep(1) def func2(): while True: print '#2!' sleep(0.5) thread.start_new_thread(myFunc, ()) thread.start_new_thread(func2, ())
http://docs.python.org/library/thread.html
Also, please don't use such bad writing style in your code. Only put spaces between non-unary operators. Or, at least don't put spaces between the periods denoting namespaces (like "time . sleep(0.25)") and the colons in your conditional statements. You can check here for a style guide for Python.
Last edited by shadwickman; Jul 10th, 2009 at 6:53 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
•
•
Join Date: Jul 2009
Posts: 7
Reputation:
Solved Threads: 0
Thanks for the suggestion!
I needed a way to wait on the thread to complete before continuing, so I wound up using threading instead. However, it's still not updating the window well.
With this setup, it's acting just the way it did before. I'd appreciate any more feedback anyone might have.
Thanks!
P.S. My coding style is on purpose. I put great emphasis on human readability, which comes out in many ways, including generous whitespace.
I needed a way to wait on the thread to complete before continuing, so I wound up using threading instead. However, it's still not updating the window well.
Python Syntax (Toggle Plain Text)
import subprocess import threading import time class Thread ( threading . Thread ): def run ( Self ): subprocess . call ( "mycommand.exe" ) External_Executable = Thread () External_Executable . start () while External_Executable . is_alive (): Window . Update () time . sleep ( 0.25 )
With this setup, it's acting just the way it did before. I'd appreciate any more feedback anyone might have.
Thanks!
P.S. My coding style is on purpose. I put great emphasis on human readability, which comes out in many ways, including generous whitespace.
Last edited by SpiritGeek; Jul 10th, 2009 at 7:54 pm.
I'd put the window updating segment in a function and call that via a thread as well. Try how that works out.
P.S. As long as your style works for you. I personally can't stand it as whitespace is good, but too much is difficult. It just makes it hard to see some of the OOP-based concepts with the periods being so spaced out.
P.S. As long as your style works for you. I personally can't stand it as whitespace is good, but too much is difficult. It just makes it hard to see some of the OOP-based concepts with the periods being so spaced out.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
Your code has a couple of issues, if i am not wrong it will not actually start using threading with your code as it is. You need to add a couple of lines
See how i have called the __init__ method on the threading.Thread class? This means that now it will run the __init__ method of the Thread class, which is needed for threading to work properly with classes.
So try replacing you current class with that and try again and see how it all goes
Hope that helps
python Syntax (Toggle Plain Text)
class Thread ( threading . Thread ): def __init__(self): threading.Thread.__init__(self) def run ( Self ): subprocess . call ( "mycommand.exe" )
So try replacing you current class with that and try again and see how it all goes

Hope that helps
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Jul 2009
Posts: 7
Reputation:
Solved Threads: 0
Well, based on the replies here, I just spent the last two days teaching myself the multiprocessing module (with inter-process queues) so I could put the UI in one process and the workload in another to solve this problem.
I was pretty much done with that when DevCoach at Dev Shed offered this: instead of Window . Update (), use wx . Yield ().
It works perfectly.
Well, at least I know how to multi-process now! LOL
Thanks, all!
I was pretty much done with that when DevCoach at Dev Shed offered this: instead of Window . Update (), use wx . Yield ().
It works perfectly.

Well, at least I know how to multi-process now! LOL
Thanks, all!
![]() |
Similar Threads
- Starting wxPython (GUI code) (Python)
- (HELP) wxPython: Update other panels from event triggered in another panel (Python)
- wxpython: can't update panel contents with toolbar events (Python)
- Writing the output of subprocess to a file (Python)
- WxPython and py2exe trouble (Python)
- PyScripter and WxPython debug session (Python)
Other Threads in the Python Forum
- Previous Thread: clonedigger installation
- Next Thread: Encoding Issues
Views: 480 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt





