tail -f con gtk

Manuel Angel Fernandez rastreador en gmx.net
Jue Mar 6 00:13:42 CET 2003


> Muéstranos el código. Con lo que cuentas no te sabría decir dónde
> tienes el problema.

Ya lo tengo solucionado.
El problema esta en que cuando haces un bucle con while... o for ... el boton que 
pulsaste para ejecutar ese codigo se queda pulsado y la aplicacion "ocupada" hasta 
que termina de ejecutarse el bucle que en mi caso era nunca ya que estaba a la 
espera de que el fichero cambiara de tamaño.

La solucion  esta en utilizar la funcion timeout de pyGTK. Esta funcion permite 
ejecutar otra funcion cada x milisegundos y sin los inconvenientes que mencionaba 
antes. Ademas permite terminarla devolviendo FALSE o mediante la funcion 
timeout_remove.

Por si alguien lo necesita os paso un pequeño programa de ejemplo que hace esto.

#!/usr/bin/env python

from gtk import *
import gtk.glade

global Numero 

class Ventana:
    def __init__(self):
        v = gtk.glade.XML("test.glade")
      
        self.ventana = v.get_widget("Test")
        self.ventana.connect("destroy", self.destroy)

        self.btn= v.get_widget("btn1")
        self.btn.connect("clicked", self.Pulsame)
        

        self.Texto=v.get_widget("Texto")
        self.Texto.connect("leave_notify_event", self.pasa_por_encima)

    def Pulsame(self,widget):
        self.Texto.set_text("Hola")
        self.timeout=timeout_add(1000, self.Escribe)        

    def Escribe(self):
        self.Numero=self.Numero+1
        if self.Numero<10:
            self.Texto.set_text("el timeout va por el "+str(self.Numero))
            return 1
        else:
            self.Texto.set_text("el timeout va por el "+str(self.Numero)+", y se acabo")
            return 0

    def pasa_por_encima(self,widget,data):
        self.Texto.set_text("Acabo de pasar")
        
    def destroy(self, widget, data=None):
        gtk.main_quit()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    ven = Ventana()
    ven.Numero=0
    ven.main()
        
Ahora el test.glade

<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkWindow" id="Test">
  <property name="visible">True</property>
  <property name="title" translatable="yes">Test</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>

  <child>
    <widget class="GtkFixed" id="fixed1">
      <property name="visible">True</property>

      <child>
	<widget class="GtkEntry" id="Texto">
	  <property name="width_request">224</property>
	  <property name="height_request">24</property>
	  <property name="visible">True</property>
	  <property name="can_focus">True</property>
	  <property name="editable">True</property>
	  <property name="visibility">True</property>
	  <property name="max_length">0</property>
	  <property name="text" translatable="yes"></property>
	  <property name="has_frame">True</property>
	  <property name="invisible_char" translatable="yes">*</property>
	  <property name="activates_default">False</property>
	</widget>
	<packing>
	  <property name="x">8</property>
	  <property name="y">8</property>
	</packing>
      </child>

      <child>
	<widget class="GtkButton" id="btn1">
	  <property name="border_width">1</property>
	  <property name="width_request">120</property>
	  <property name="height_request">24</property>
	  <property name="visible">True</property>
	  <property name="can_default">True</property>
	  <property name="has_default">True</property>
	  <property name="can_focus">True</property>
	  <property name="label">gtk-dialog-info</property>
	  <property name="use_stock">True</property>
	  <property name="relief">GTK_RELIEF_NORMAL</property>
	</widget>
	<packing>
	  <property name="x">56</property>
	  <property name="y">40</property>
	</packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>


> Puede ser que estés ejecutando el programa dentro del pythonwin. No
> uso gtk en windows, pero siempre se ha dicho que el pythowin no se
> lleva bien con otros toolkits que no sea el MFC.
Cierto, no se puede ejecutar desde pythonwin


Un saludo.

		-=|Manuel Angel Fernández|=-




Más información sobre la lista de distribución Python-es