Hi folks

I started with Python yesterday and discovered this great work you've done.

I have a problem with a code I'm using to learn, I was wondering if you
would mind giving it a look, because I'm stuck.

The problem, as you can see, is that the Thread doesn't get executed until
the main program ends. I don't know what could be going wrong. Here's the
code.

I'm attaching the glade file as well.

If this is not the proper list to ask this, please forgive me (and point me
where go to if you can).

Thanks in advance guys

#!/usr/bin/python
# -*- coding: utf-8 -*-

import pygtk
pygtk.require ("2.0")
import gtk, gtk.glade
import os
import re
import time
import sys
from threading import Thread
from wave import open as waveOpen
from ossaudiodev import open as ossOpen
from ossaudiodev import AFMT_S16_LE

class Ping(Thread):

   lifeline = {'packet':re.compile(r"(\d)% packet loss"),'connectivity':
re.compile(r"(\d)% received")}

   def __init__(self,sHost,widget,type,beep,cback):

       Thread.__init__(self)
       self.sHost=sHost
       self.buffer=widget
       self.beep=beep
       self.type=type
       self.callback=cback

   def ping(self):
       pingaling = os.popen("ping -q -c1 "+self.sHost,"r")
       keep = 1
       while keep == 1:
           line = pingaling.readline()
           if not line: break
           if self.type=="Conn":
               igot = re.findall(self.lifeline["connectivity"],line)
           else:
               igot = re.findall(self.lifeline["packet"],line)
           if igot:
              return int(igot[0])

   def run(self):
       pinglist = []
       self.keepPinging = 1
       while self.keepPinging:
          status = self.ping()
          if status != -1:
              if self.type=="Conn":
                  sText = 'Connection state to %s is %s\n' % (self.sHost,
status)
              else:
                  sText = '# of packets lost from %s is %s\n' % (
self.sHost, status)
              self.callback(self,sText)
              if self.beep:
                  self.playsound("generic.wav")


   def playsound(file):
       s = waveOpen( sys.path[0]+"/"+file, "rb")
       dsp = ossOpen("/dev/dsp","w")
       (nc,sw,fr,nf,comptype, compname) = s.getparams( )
       dsp.setparameters(AFMT_S16_LE, nc, fr)
       data = s.readframes(nf)
       s.close()
       dsp.writeall(data)
       dsp.close()

class App:

   def __init__(self):
       self.glade = gtk.glade.XML(" gpyng.glade")
       self.glade.signal_autoconnect(self)
       self.glade.get_widget("main").show_all()


   def show_dialog(toplevel,sTitle,sText):

       dia = gtk.Dialog(sTitle)
       dia.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
       dia.vbox.pack_start(gtk.Label(sText))
       dia.set_default_response(gtk.RESPONSE_OK)
       dia.show_all()
       response = dia.run()
       dia.destroy()

   def on_main_delete_event(self, widget, event):
       self.thr.keepPinging = 0
       gtk.main_quit()

   def ping_callback(self,caller,sText):
       print "Callbacking..."
       buffer = self.glade.get_widget("console").get_buffer()
       iter = buffer.get_end_iter()
       buffer.place_cursor(iter)
       buffer.insert (iter, sText)

   def on_togglebuttonStart_clicked(self, event):
       if self.glade.get_widget("togglebuttonStart").get_active():
           if self.glade.get_widget("txtHost").get_text() == "":
               self.show_dialog("Faltan datos","Debe ingresar un host")
               self.glade.get_widget("togglebuttonStart").set_active(False)
               return
           self.keepPinging = 1
           
self.glade.get_widget("console").get_buffer().insert_at_cursor("Iniciando
--> "+time.ctime()+"\n")
           self.glade.get_widget("togglebuttonStart").set_label("Terminar")

           sHost = self.glade.get_widget("txtHost").get_text()

           self.thr = Ping(sHost,
self.glade.get_widget("console").get_buffer(),
self.glade.get_widget("radiobuttonConn").get_active() and "Conn" or
"Packet", self.glade.get_widget("checkbuttonBeep").get_active(),
self.ping_callback)
           self.thr.start()
       else:
           if self.keepPinging:

self.glade.get_widget("console").get_buffer().insert_at_cursor("Terminado
--> "+time.ctime()+"\n")

self.glade.get_widget("togglebuttonStart").set_label("Comenzar")

               self.keepPinging = 0
               self.thr.keepPinging = 0

if __name__ == "__main__":
   try:
       App()
       gtk.main()
   except KeyboardInterrupt:
       pass


--
Rodrigo

Attachment: gpyng.glade
Description: Binary data

_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to