Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: threads with gtk gui problem

Reply
Thread Tools

Re: threads with gtk gui problem

 
 
andy baxter
Guest
Posts: n/a
 
      05-14-2011
On 14/05/11 14:12, Andy Baxter wrote:
> Hi,
>
> I'm working on adding a Gtk GUI to a python program. Its main function
> is to read raw data from an arduino board over USB, and convert it to
> MIDI note/controller events to be sent to another program. I've had it
> working fine with just a command line interface, but when I replaced
> the command line with a Gtk interface, I started having problems
> getting the thread that reads the USB port to run.
>
> I'm not sure what the custom is on this list for pasting code - It's a
> long program so I don't want to paste the whole thing.
>
> The sequence of events I've coded is:
>
> - the program starts.
> - the port reader thread (which is a threading.Thread subclass) is
> initialised.
> - the object which controls the interface is initialised (i.e. the
> glade file is loaded.)
> - the port reader is started (i.e. the 'start' method is called, which
> calls the 'run' method in a thread).
> - then the gtk main loop is run.
>
> The behaviour I'm getting is that the port reader either fails to
> start, or stops running at the point where it tries to initialise the
> serial port. It then does nothing until I close the main window, at
> which point it starts running again.
>
> The port reader's run method begins like this:
>
> # the method called by the thread superclass to run the main loop
> of the thread.
> def run(self):
> # main loop of thread.
> print "Starting port reader."
> fhan=serial.Serial(port=keeper.config['usbPort'],
> baudrate=keeper.config['usbBaud'], timeout=0.1)
> print "1"
> fhan.open()
> print "2"
> seq=PySeq() # the sequencer library object
> port=seq.createOutPort(keeper.config['midiPortName']) # make a
> midi out port.
> midich=keeper.config['midich']
> print "3"
> while True:
> inbuf=[]
> print ".",
> char=fhan.read(1)
> if self.quit:
> fhan.close()
> return
> if len(char)==0: continue
> ... (code to process the character read in)
>
> ('keeper' is a global object which stores the config data and
> references to a few key objects).
>
> When you start the program, the thread stops either before the first
> print statement, or on the line which initialises the serial port (
> fhan=serial.Serial(...) ). '1', '2', and '3' only get printed /after/
> you close the gtk main window.
>


I've verified that the problem is due to the gtk main loop interfering
with the thread I've started. If I use this code:

if __name__ == "__main__":
print "Starting mapper"
keeper=Keeper() # initialise the keeper. (A global object which
holds references to key objects)
keeper.start() # tell the keeper to start the port reader.
for n in xrange(10): # wait for 10 seconds.
time.sleep(1)
print "x"
#gtk.main() # start the gtk main loop.
keeper.stop() # tell the keeper to shut things down.

the program runs correctly. I.e. values are read from the usb port when
they come in.

if I use this code:

if __name__ == "__main__":
print "Starting mapper"
keeper=Keeper() # initialise the keeper. (A global object which
holds references to key objects)
keeper.start() # tell the keeper to start the port reader.
#for n in xrange(10):
# time.sleep(1)
# print "x"
gtk.main() # start the gtk main loop.
keeper.stop() # tell the keeper to shut things down.

The port reader thread blocks at the point it tries to open the serial
port. I.e. 'Starting port reader' is printed straight away, but '1',
'2', and '3' are only printed after you close the gtk main window.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
threads with gtk gui problem Andy Baxter Python 0 05-14-2011 01:12 PM
Ruby GTK Fortune Cookies (gtk-fortune-0.1.1) Alex Shulgin Ruby 1 11-21-2007 01:05 AM
GTK+, segmentation fault in gtK-editable_get_chars zombek@gmail.com C Programming 2 05-29-2007 03:27 PM
What's the best GUI toolkit in Python,Tkinter,wxPython,QT,GTK? Wilk Python 12 03-30-2005 12:01 PM
compiling ruby under windows (+ gtk stuff, is the gtk list dead?) Gergely Kontra Ruby 2 06-18-2004 10:20 PM



Advertisments