2011-11-14 12 views
5
  • Python 3.2.2
  • gtk3 3.2.2
  • pitón-gobject 3.0.2

Estoy tratando de mostrar una GUI y hacer un poco de trabajo en segundo plano. Según tengo entendido, debería verse algo como esto:Python. Haciendo un poco de trabajo en el fondo con GTK GUI

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 


import time 
from threading import Thread 
from gi.repository import Gtk, Gdk 

class Gui(): 
     def run(self): 
       self.Window = Gtk.Window() 
       self.Window.set_border_width(8) 
       self.Window.set_title("Некий GUI") 
       self.Window.connect('destroy', lambda x: self.stop()) 

       self.outBut = Gtk.Button.new_from_stock(Gtk.STOCK_OK) 
       self.outBut.set_size_request(150, 35) 
       self.outBut.connect('clicked', lambda x: self.passfun) 
       self.Window.add(self.outBut) 

       self.Window.show_all() 

     def stop(self): 
       Gtk.main_quit() 

     def passfun(self): 
       pass 

class LoopSleep(Thread): 
     def run(self): 
       i = 1 
       while True: 
         print(i) 
         i = i + 1 
         #time.sleep(1) 



gui = Gui() 
gui.run() 

loopSleep = LoopSleep() 
loopSleep.start() 

Gdk.threads_init() 
Gdk.threads_enter() 
Gtk.main() 
Gdk.threads_leave() 

Pero no funciona. Se producen varios ciclos cuando presiona el botón. Y el ciclo se ejecuta después de que la ventana está cerrada. Pero no juntos.

¿Qué hago mal?

+0

Debe incluir el código con su pregunta. –

+0

Uso pastebin, porque no puedo entender cómo funciona el código de trabajo aquí. – Atterratio

+1

Lo pega, lo selecciona y presiona el botón de código en el editor. –

Respuesta

8

No puedo afirmar que sea un experto en el enmascaramiento de python ni gtk3, pero después de jugar un poco con su ejemplo, encontré algo que parece funcionar de la manera que usted lo desea. En lugar de subclasificar el hilo, uso threading.start (target = loop_sleep) y lo coloco dentro de Gui.

Glib.threads_init() también parecen ser necesarios.

#!/usr/bin/env python3 
from gi.repository import Gtk,Gdk, GLib 
import threading 
import time 

class Gui(Gtk.Window): 
    def __init__(self): 
     self.Window = Gtk.Window() 
     self.Window.set_border_width(8) 
     self.Window.set_title("Некий GUI") 
     self.Window.connect('destroy', lambda x: self.stop()) 

     self.outBut = Gtk.Button.new_from_stock(Gtk.STOCK_OK) 
     self.outBut.set_size_request(150, 35) 
     self.Window.connect('destroy', lambda x: self.stop()) 
     self.Window.add(self.outBut) 

     self.Window.show_all() 
     threading.Thread(target=loop_sleep).start() 

    def stop(self): 
     Gtk.main_quit() 

    def passfun(self): 
     pass 

def loop_sleep(): 
     i = 1 
     while True: 
      print(i) 
      i = i + 1 
      #time.sleep(1) 



app = Gui() 
GLib.threads_init() 
Gdk.threads_init() 
Gdk.threads_enter() 
Gtk.main() 
Gdk.threads_leave() 
Cuestiones relacionadas