0

These are my first steps with python and desktop programming. I worked with web all my life and now I need to do threading. I tried work with tkinter, pyqt and now gtk(father of tkinter) with glade xml.

I tried different modules because I was failing every time on the same problem. The interface has an infinite loop so I read a little bit about it on Google and its my first time working with python, desktop and threading so here's my code could someone say what is wrong and why?

# -*- coding: utf-8 -*-
#Parallelism modulos
import threading
import Queue
#GUI modulos
import gtk, sys
#webservice modulos
import urllib2, json, urllib
#rfid modulos
import MFRC522
import signal
#GPIO modulos
import RPi.GPIO as GPIO
#sleep modulos
from time import sleep
#database modulos
import psycopg2

class Janela(threading.Thread):

    def on_window_destroy(self, data=None):
        gtk.main_quit()

    def __init__(self, queue):
        threading.Thread.__init__(self)
        self._queue = queue
    def run(self):
        builder = gtk.Builder()
        builder.add_from_file("interface.glade") 
        self.window = builder.get_object("window1")
        '''aluno informação'''
        self.alunoimage = builder.get_object("alunoimage")
        self.label = builder.get_object("nomecrianca")

        '''Responsavel informação'''
        builder.connect_signals(self)   



        imagefile = "anderson_caricatura.png" 
        self.alunoimage.set_from_file(imagefile)
        self.label.set_text("Anderson Santana")
        self.window.show()        
        gtk.main()

class RfID():

    def __init__(self):
        a = "343511711127"
        self.read()


    def beep(self):
        GPIO.setup(11, GPIO.OUT)
        GPIO.output(11, False)
        sleep(0.05)
        GPIO.output(11, True)
        sleep(0.05)
        GPIO.output(11, False)
        sleep(0.05)
        GPIO.output(11, True)
    def regulariza_cartao(self,cartao):
        string = ''.join(str(e) for e in cartao);
        return string

    def consulta_web_service(self,cartao):
        req =        urllib2.Request("http://192.168.1.161/ieduca/TheClientRfid.php?cartao="+cartao)
        response = urllib2.urlopen(req)
        the_page = response.read()
        return the_page


    def end_read(signal, frame):
        MIFAREReader = MFRC522.MFRC522()
        global continue_reading
        continue_reading = False
        print "Ctrl+C captured, ending read."
        MIFAREReader.GPIO_CLEEN()

    def read(self):


        continue_reading = True
        MIFAREReader = MFRC522.MFRC522()
        while continue_reading:

            (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
            (status,backData) = MIFAREReader.MFRC522_Anticoll()
            if status == MIFAREReader.MI_OK:
                self.beep()
                string = self.regulariza_cartao(backData)
                consome = self.consulta_web_service(string)

                jsonreaded = json.loads(consome)
                print "Tem responsavel? ",jsonreaded['have_responsavel']
                print "nome do estudante: ",jsonreaded['estudante_nome']
                print "estudante imagem: ",jsonreaded['estudante_imagem']
                print "imagem caminho: ",jsonreaded['estudante_caminho']

                urllib.urlretrieve("http://192.168.1.161/ieduca/"+jsonreaded['estudante_caminho']+jsonreaded['estudante_imagem'], jsonreaded['estudante_imagem'])
                print "baixou a imagem"
                queue = Queue.Queue()
                print "abriu comunicador"
                worker = Janela(queue)
                print "instanciou a janela"
                worker.start()
                print "iniciou a janela"
                queue.put('quit')
                print "pede pra sair 02"

                print "rala!"
def run():



    GUI = RfID()



run()

this code seems with what is happening to my code

import threading
from time import sleep

#lets pretend this class is the rfid class and will be the first to run
class RFid(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        i=0
        d=True
        while d:
            print "oi"
            i = i+1
            print i
            if i==10:
                t2 = Gtk()#after some conditions in it while other class will run this class is the gtk (window, label, images)obs:no buttons
                t2.start()
                t2.join()
                sleep(5)
                del t2
                #self.run() (or something or something meaning it)
            #for example 
            ##if ctrl+c:
            ##d = False

class Gtk(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        while True:#here on the gtk theres a infinity while never ends but it should close the window after some secs and than back to while of first class  
            print "ola"

#to resume the rfid will open a window with information of the owner of the rfidcard on scream
#but when the information are opened gtk enter on a infinity loop wainting for a event it will never happen
#when it should close after 5 secs and when other card's owner pass show other window and this forever...

def main():
    t1 = RFid()
    t1.start()


if __name__ == '__main__':
    main()

i hope you understand now

  • 1
    You're going to have to give more detail about 'what is wrong'... And have you tried reducing the code to the simplest possible example of your problem? – DisappointedByUnaccountableMod Mar 15 '16 at 14:16
  • gtk is not the "father of tkinter". They are completely unrelated. – Bryan Oakley Mar 15 '16 at 14:20
  • barny, yes this is the simplest i need the problem its when i open a window with gtk seems the worker are not on a new thread it stops the app and i cant read rfids anymore i would like the gtk.main didnt stop my application – Sandro Matos Mar 15 '16 at 14:32
  • 1
    As soon as you import a library not in Python standard library, it's too complicated - no-one else can run the code to try reproduce the problem you are having. Also, you are unlikely to get help debug your finished application - you can do that yourself. To stand best chance of getting help, write the smallest, simplest example code that shows the problem, perhaps it has a thread which prints a number once per second on the console and in the other thread has a window with a QUIT button that exits the app. Wouldn't be surprised if you solved the problem while trying to create the example. – DisappointedByUnaccountableMod Mar 15 '16 at 15:08
  • well this new code above show what is happening (or i think is happening) – Sandro Matos Mar 15 '16 at 17:16
  • Have you looked at similar questions such as http://stackoverflow.com/questions/12094555/running-computation-in-background-thread-python-pygtk and http://stackoverflow.com/questions/11923008/threading-in-gtk-python - these are results from goggle search for _python gtk example background thread_ which took about ten seconds to produce these results and many more including http://faq.pygtk.org/index.py?req=show&file=faq20.006.htp . Actually, searching is often a lot quicker than posting questions here... – DisappointedByUnaccountableMod Mar 15 '16 at 17:54

0 Answers0