Foros del Web » Programando para Internet » Python »

import threading

Estas en el tema de import threading en el foro de Python en Foros del Web. buenas tengo un problema con hilos de un programa :S .....en la linea 68 creo la funcion del hilo luego lo conecto con el boton ...
  #1 (permalink)  
Antiguo 11/09/2012, 09:04
 
Fecha de Ingreso: septiembre-2012
Mensajes: 53
Antigüedad: 11 años, 7 meses
Puntos: 0
import threading

buenas tengo un problema con hilos de un programa :S .....en la linea 68 creo la funcion del hilo luego lo conecto con el boton y la gui ahora no se pega por el proceso de la linea 75... pero luego de regresar a la gui no me abre la ventana de dialogo y se cierra inesperada mente y marca en la consola:
Código:
Violación de segmento
este es codigo si me pueden ayudar plzzzzzz gracias...

Código Python:
Ver original
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import random
  5. import Image
  6. import ImageFont
  7. import ImageDraw
  8. import ImageFilter
  9. import gtk
  10. import pygtk
  11. import os
  12.  
  13. import threading
  14. gtk.gdk.threads_init()
  15. #--------------------------imagen construir--------------------------------
  16. def gen_random_word(wordLen=6):
  17.     allowedChars = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWZYZ0123456789"
  18.     word = ""
  19.     for i in range(0, wordLen):
  20.         word = word + allowedChars[random.randint(0,0xffffff) % len(allowedChars)]
  21.     return word
  22.    
  23. def gen_captcha(text, fnt, fnt_sz, file_name, fmt='JPEG'):      
  24.     fgcolor = random.randint(0,1)
  25.     bgcolor = fgcolor ^ 0xffffff
  26.     font = ImageFont.truetype(fnt,fnt_sz)
  27.     dim = font.getsize(text)
  28.     im = Image.new('RGB', (dim[0]+5,dim[1]+5), bgcolor)
  29.     d = ImageDraw.Draw(im)
  30.     x, y = im.size
  31.     r = random.randint
  32.     for num in range(100):
  33.         d.rectangle((r(0,x),r(0,y),r(0,x),r(0,y)),fill=r(0,0xffffff))
  34.     d.text((3,3), text, font=font, fill=fgcolor)
  35.     im = im.filter(ImageFilter.EDGE_ENHANCE_MORE)
  36.     im.save(file_name, format=fmt)
  37.  
  38.  
  39.  
  40. #--------------------------------------------------------------------------
  41. class PackBox1:
  42.     def __init__(self):
  43.        
  44.         self.word = gen_random_word()
  45.         gen_captcha(self.word.strip(), '/usr/share/fonts/truetype/freefont/FreeSansBoldOblique.ttf', 25, "test.jpg")
  46.         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  47.         window.set_title("-* C A N A I M A *-")
  48.         window.set_border_width(10)
  49.         window.set_size_request(550, 200)
  50.         window.set_resizable(False)
  51.  
  52.        
  53.         def imagen_box2(homogeneous, spacing, expand, fill, padding):
  54.    
  55.             caja = gtk.HBox(homogeneous, spacing)
  56.             caja.set_border_width(5)
  57.    
  58.             self.image = gtk.Image()
  59.             self.image.set_from_file('test.jpg')
  60.             #self.image.connect("clicked", self.refresh_captcha)
  61.             caja.pack_start(self.image, True, False,0)
  62.             self.image.show()
  63.    
  64.             return caja
  65.        
  66.         def make_box1(homogeneous, spacing, expand, fill, padding):
  67.            
  68.             def clic_boton2(widget):
  69.                 hilo = threading.Thread(target=captcha_fun, args=(widget))
  70.                 hilo.start()
  71.            
  72.             def captcha_fun(widget):
  73.                 if  texto.get_text() != self.word:
  74.    
  75.                     systema= os.system("gedit /tmp/Documento.txt")
  76.                    
  77.                     gtk.gdk.threads_enter()
  78.                     md=gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE, message_format="El valor introducido no coincide con el captcha intente de nuevo")
  79.                     md.run()
  80.                     md.destroy()
  81.                     self.refresh_captcha()
  82.                     gtk.gdk.threads_leave()
  83.  
  84.                 else:  
  85.                     gtk.gdk.threads_enter()
  86.                     md=gtk.MessageDialog(parent=None, flags=0, buttons=gtk.BUTTONS_OK, message_format="El valor introducido es Correcto")
  87.                     md.run()
  88.                     md.destroy()
  89.                     self.refresh_captcha()
  90.                     gtk.gdk.threads_leave()
  91.  
  92.             caja = gtk.HBox(homogeneous, spacing)
  93.             caja.set_border_width(10)
  94.            
  95.             etiqueta = gtk.Label("Introduzca el valor")
  96.             #etiqueta.set_alignment(0,0)
  97.             caja.pack_start(etiqueta, False, False, 30)
  98.             etiqueta.show()
  99.    
  100.             texto = gtk.Entry(10)
  101.             #texto.connect("activate", enter_callback)
  102.             caja.pack_start(texto, False, False, 10)
  103.             texto.show()
  104.    
  105.             boton = gtk.Button(stock=gtk.STOCK_OK)
  106.             boton.connect("clicked", clic_boton2)
  107.             caja.pack_start(boton, True, True, 40)
  108.             boton.show()
  109.             return caja
  110.        
  111.         def make_box3(homogeneous, spacing, expand, fill, padding):
  112.    
  113.             caja = gtk.HBox(homogeneous, spacing)
  114.             caja.set_border_width(10)
  115.            
  116.             boton = gtk.Button(stock=gtk.STOCK_CLOSE)
  117.             boton.connect("clicked", gtk.mainquit)
  118.             caja.pack_start(boton, gtk.TRUE, gtk.TRUE, 70)
  119.             boton.show()
  120.             return caja
  121.        
  122.         box1 = gtk.VBox(False, 0)
  123.        
  124.         box2 = imagen_box2(False, 0, False, False,0)
  125.         box1.pack_start(box2, False, False, 0)
  126.         box2.show()
  127.        
  128.         box2 = make_box1(False, 0, False, False,0)
  129.         box1.pack_start(box2, False, False, 0)
  130.         box2.show()
  131.        
  132.         box2 = make_box3(False, 0, False, False,0)
  133.         box1.pack_start(box2, False, False, 20)
  134.         box2.show()
  135.        
  136.         box1.show()
  137.         window.add(box1)
  138.         window.show()
  139.        
  140.     def refresh_captcha(self):
  141.         self.word = gen_random_word()
  142.         gen_captcha(self.word.strip(), '/usr/share/fonts/truetype/freefont/FreeSansBoldOblique.ttf', 25, "test.jpg")
  143.         self.image.set_from_file('test.jpg')
  144.    
  145. def main():
  146.     gtk.main()
  147.     return 0
  148.            
  149. if __name__ == '__main__':
  150.     packbox1 = PackBox1()
  151.     main()
  #2 (permalink)  
Antiguo 11/09/2012, 09:18
Avatar de mcun  
Fecha de Ingreso: octubre-2010
Ubicación: tras la pantalla
Mensajes: 466
Antigüedad: 13 años, 6 meses
Puntos: 55
Respuesta: import threading

El problema es que no puedes directamente modificar un proceso GUI desde fuera, cada lib de interface gráfica tiene su propia forma de manejar hilos ..
aquí tienes una explicación y un ejemplo

http://pythonmania.wordpress.com/200...eads-en-pygtk/
  #3 (permalink)  
Antiguo 11/09/2012, 09:52
 
Fecha de Ingreso: septiembre-2012
Mensajes: 53
Antigüedad: 11 años, 7 meses
Puntos: 0
Respuesta: import threading

Es el hilo funciona corre la gui y el proceso y no se queda guindada pero cuando le coloco una ventana de dialogo después del proceso se guinda...

Etiquetas: gui, import, programa, threading
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:52.