[Tutor] threading problem in GUI

nephish nephish at xit.net
Tue Sep 6 21:06:36 CEST 2005

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.
>># 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.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
>>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,
ok, i am still kinda stuck.
im posting what i think is necessary, because the whole thing is rather 
so here goes.


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


def main(self):

def on_StartEnginesButton_clicked(self, widget, *args):
        print "on_StartEnginesButton_clicked called with self.%s" % 
        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)       
        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):
                    b = ser.read(1)
                    b = ord(b)
                    if (b < 64 )or (b > 127):
                    c = ser.read(1)
                    c = ord(c)
                    if c < 92:
                    d = ser.read(1)
                    d = ord(d)
                    if d < 128:
                    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')      
                    Input1Data = 
                    Input1Iter = self.Input1Buffer.get_end_iter()
                    self.Input1Buffer.insert(Input1Iter, Input1Data)
                    f = open('/home/piv/PivData/tmp/Serial/'+str(i), 'w')


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.


More information about the Tutor mailing list