Il 04/08/2014 18:18, Marco Giusti ha scritto:
Questo fa' sì che non appena il main loop non ha più niente da fare,
chiama la funzione "do_something_with_the_line".
Ciao
grazie del suggerimento.
Sto provando in vari modi ma la situazione migliore che ho ottenuto è quella del codice sotto: Funziona (stampa la riga che arriva dalla seriale) se continuo a far fare qualcosa alla GUI altrimenti no.
Ho provato a forzare con cose tipo:

while gtk.events_pending():
    gtk.main_iteration()

Ma non ho ottenuto miglioramenti.
Avete suggerimenti?

import gtk

import sys

import threading

import serial

#import gtk.glade

import glib

class Window:

    def __init__(self):

        builder = gtk.Builder()

        builder.add_from_file("gui.glade")

        self.win = builder.get_object("win")

        self.win.connect("destroy", self.exit)

        self.ssbutton=builder.get_object('ssbutton')

        self.ssbutton.connect('clicked',self.on_ssbutton_clicked)

        self.text=builder.get_object('testo')

        self.text.set_text("CIAO")

        self.title=builder.get_object('title')

        self.title.set_text("test")

        self.win.show_all()

    def main(self):

        gtk.main()

    def exit(self,widget,data=None):

        print 'quit'

        gtk.main_quit()

        sys.exit()

    def on_ssbutton_clicked(self,event):

        print str(self.ssbutton.get_active())

class Thread1 (threading.Thread):

    def __init__(self, threadID, name):

        threading.Thread.__init__(self)

        self.threadID = threadID

        self.name = name

    def run(self):

        win = Window()

        win.main()

class Thread2 (threading.Thread):

    def __init__(self, threadID, name):

        threading.Thread.__init__(self)

        self.threadID = threadID

        self.name = name

    def stampa(self,riga):

        print riga

    def run(self):

        self.ser=serial.Serial("/dev/ttyACM0",9600)

        while True:

            self.a=self.ser.readline()

            if self.a!="":

                glib.idle_add(self.stampa, self.a)

                while gtk.events_pending():

                    gtk.main_iteration()

thread1 = Thread1(1, "GUI")
thread2 = Thread2(2, "Serial")
thread1.start()
thread2.start()


Grazie
Matteo P
_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

Rispondere a