[Tutor] threading problem in GUI
Pierre Barbier de Reuille
pierre.barbier at cirad.fr
Tue Sep 6 22:43:27 CEST 2005
Ok, comments inside your code ...
nephish a écrit :
> Pierre Barbier de Reuille wrote:
>
>> nephish a écrit :
>>
>>
>>> Pierre Barbier de Reuille wrote:
>>>
>>> [...]
>>> ok, i am still having a little problem understanding.
>>> tried it but i don't know if i have things set in the right order.
>>>
>>>
>>> gtk.gdk.threads_init()
>>> # Here initialize what you want
>>> [...]
>>> # Launch the Gtk loop
>>> gtk.gdk.threads_enter() # Unneeded if you don't want to call GUI
>>> # functions from other threads
>>> gtk.main()
>>> gtk.gdk.threads_leave() # Needed only with threads_enter
>>>
>>> at the part where you wrote # Here initialize what you want
>>> [...]
>>> is that where i define the function that will run the thread?
>>>
>>> i get the part about having the enter and leave
>>> i just seem to have a hang up (so to speak) with where the function gets
>>> defined.
>>> do i need to build it as a class like the example in the link you sent?
>>>
>>
>>
>> Well, no you don't need to create a class.
>>
>> As for the place where to put your function, it's not a problem, [...]
>> is the place where you will *execute* some initialization code (if
>> needed) before you launch your interface. That's all (typically that's
>> where you will instanciate your widgets).
>>
>>
>>
>>> thanks for your help on this
>>>
>>>
>>
>> Your welcome,
>>
>> Pierre
>>
>>
>>
> ok, i am still kinda stuck.
> im posting what i think is necessary, because the whole thing is rather
> long.
> so here goes.
>
> #!/usr/bin/python
>
> import os
> import time
> from time import strftime
>
> import sys
> import gtk
> import pygtk
> import serial
> import threading
> from threading import Thread
> import tokenize
> import gtk.glade
> import weakref
> import inspect
> import re
>
>
> gtk.gdk.threads_init()
>
> def main(self):
> gtk.gdk.threads_enter()
> gtk.main()
> gtk.gdk.threads_leave()
>
> def on_StartEnginesButton_clicked(self, widget, *args):
> print "on_StartEnginesButton_clicked called with self.%s" %
> widget.get_name()
> Input1Iter = self.Input1Buffer.get_end_iter()
> Input2Iter = self.Input2Buffer.get_end_iter()
> Input1Data = 'Reading Serial device ttyS14 \n'
> Input2Data = 'Reading Serial device ttys15 \n'
> self.Input1Buffer.insert(Input1Iter, Input1Data)
> self.Input2Buffer.insert(Input2Iter, Input2Data)
> time.sleep(2)
> def Serial1(): print 'running serial 1'
> ser = serial.Serial('/dev/ttyS15', 2400, timeout=None)
> loopy = 1
> i = 1
> while loopy < 5: for x in range(5):
> i = i + 1 a =
> ser.read(1)#read one byte a = ord(a) # change
> byte to integer
> if (a < 64 )or (a > 127):
> break b = ser.read(1)
> b = ord(b)
> if (b < 64 )or (b > 127):
> break
> c = ser.read(1)
> c = ord(c)
> if c < 92:
> break
> d = ser.read(1)
> d = ord(d)
> if d < 128:
> break
> Sensor_ID = (a & 63) + (b & 63) * 64 + (c & 1) * 4096
> Status = (c & 62) / 2 + (d & 63) * 32
> c = int(c)
> d = int(d)
> x_Now = strftime('%Y-%m-%d %H:%M:%S')
> gtk.threads_enter()
> Input1Data =
> str(Sensor_ID)+'\t'+str(Status)+'\t--------->\t'+x_Now+'\n'
> Input1Iter = self.Input1Buffer.get_end_iter()
> self.Input1Buffer.insert(Input1Iter, Input1Data)
> gtk.threads_leave()
> f = open('/home/piv/PivData/tmp/Serial/'+str(i), 'w')
>
> f.write(str(Sensor_ID)+'\n'+str(c)+'\n'+str(d)+'\n'+str(Status)+'\n'+x_Now)
>
> f.close()
> Thread.start(Serial1())
hehehe ... got it !
Serial1() just call the function ... it will be evaluated and then the
result will be sent to Thread.start ...
So try:
Thread.start(Serial)
>
> thanks, i am learning from several sources. two books from O'Reilly, and
> a dozen or so websites,
> not everybody does everything the same way . if something here looks
> alarming, please let me know.
>
> thanks
>
--
Pierre Barbier de Reuille
INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France
tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68
More information about the Tutor
mailing list