Foros del Web » Programando para Internet » Python »

Problemas con las ventanas en gtk

Estas en el tema de Problemas con las ventanas en gtk en el foro de Python en Foros del Web. Saludos amigos, hoy tengo el siguiente problema: Al momento de cerrar una ventana aparece un mensaje preguntando si desea salir de la interfaz, si da ...
  #1 (permalink)  
Antiguo 17/01/2012, 08:23
 
Fecha de Ingreso: noviembre-2011
Mensajes: 12
Antigüedad: 12 años, 5 meses
Puntos: 0
Problemas con las ventanas en gtk

Saludos amigos, hoy tengo el siguiente problema: Al momento de cerrar una ventana aparece un mensaje preguntando si desea salir de la interfaz, si da clic en SI se cancelan los procesos y si da clic en NO debería poder continuar en la interfaz, el problema es que si doy clic en NO se sale de igual forma. Estoy usando el "delete-event" para conectar el mensaje, probé con otros y este es el que mejor me funciona. Aquí mi código de prueba...
Código Python:
Ver original
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import gtk
  5. from subprocess import Popen, PIPE, STDOUT
  6.  
  7. def make_box(homogeneous, spacing, expand, fill, padding):
  8.    
  9.    caja = gtk.HBox(homogeneous, spacing)
  10.    caja.set_border_width(10)
  11.    
  12.    etiqueta = gtk.Label("Nombre")
  13.    caja.pack_start(etiqueta, False, False, 0)
  14.    etiqueta.show()
  15.    
  16.    texto1 = gtk.Entry(10)
  17.    caja.pack_start(texto1, False, False, 16)
  18.    texto1.show()
  19.  
  20.    return caja
  21.  
  22.  
  23. class PackBox1:
  24.    
  25.    def delete_event(self, widget, event, data=None):
  26.        gtk.main_quit()
  27.        return False
  28.        
  29.    def __init__(self):
  30.        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  31.        self.window.connect("delete_event", close)
  32.        self.window.set_border_width(10)
  33.            
  34.        box1 = gtk.VBox(False, 0)
  35.        
  36.        box2 = make_box(False, 0, False, False, 0)
  37.        box1.pack_start(box2, False, False, 0)
  38.        box2.show()
  39.        
  40.        button_box = gtk.HButtonBox()
  41.        button_box.set_layout(gtk.BUTTONBOX_SPREAD)
  42.        
  43.        aceptar = gtk.Button(stock=gtk.STOCK_OK)
  44.        button_box.pack_start(aceptar, False, False)
  45.        box1.pack_start(button_box, False, False, 0)
  46.        aceptar.show()
  47.        button_box.show()
  48.        
  49.        button = gtk.Button("Salir")
  50.        button.connect("clicked", close)
  51.        button_box.pack_start(button, True, False, 0)
  52.        box1.pack_start(button_box, True, False, 0)
  53.        
  54.        self.window.add(box1)
  55.        
  56.        button.show()
  57.        button_box.show()
  58.        
  59.        box1.show()
  60.        self.window.show()
  61.        
  62.  
  63. def close(self, widget=None):
  64.     md=gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_QUESTION, buttons=gtk.BUTTONS_YES_NO, message_format="Al cerrar, todos los procedimientos que ha realizado serán eliminados.\n ¿Desea salir de la aplicación?")
  65.     md.set_title('Cerrar')
  66.     respuesta = md.run()
  67.     md.destroy()
  68.        
  69.     if respuesta == gtk.RESPONSE_YES:
  70.         x2= Popen(["pkill lb"], shell=True, stdout=PIPE)
  71.         x3= Popen(["pkill live-build"], shell=True, stdout=PIPE)
  72.         self.destroy()
  73.         gtk.main_quit()
  74.        
  75.          
  76. def main():
  77.     gtk.main()
  78.     return 0
  79.  
  80. if __name__ =="__main__":
  81.     packbox1 = PackBox1()
  82.     main()
  #2 (permalink)  
Antiguo 17/01/2012, 14:32
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Problemas con las ventanas en gtk

Código Python:
Ver original
  1. #!/usr/bin/python
  2. import gtk
  3.  
  4. class PyApp(gtk.Window):
  5.     def __init__(self):
  6.         super(PyApp, self).__init__()
  7.        
  8.         self.set_size_request(250, 100)
  9.         self.set_position(gtk.WIN_POS_CENTER)
  10.         self.set_title("Quit example")
  11.         self.connect("delete_event", self.on_delete)
  12.         self.connect("destroy", gtk.main_quit)
  13.        
  14.         quit_button = gtk.Button("Quit")
  15.  
  16.        
  17.         quit_button.connect("clicked", self.on_quit)
  18.        
  19.        
  20.         self.add(quit_button)
  21.         self.show_all()
  22.    
  23.     def __quit(self):
  24.         md = gtk.MessageDialog(self,
  25.             gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION,
  26.             gtk.BUTTONS_YES_NO, "Are you sure to quit?")
  27.         response = md.run()
  28.         md.destroy()
  29.         return response == gtk.RESPONSE_YES
  30.    
  31.     def on_delete(self, widget, data=None):
  32.         return not self.__quit()
  33.    
  34.     def on_quit(self, widget):
  35.         if self.__quit():
  36.             self.destroy()
  37.    
  38.    
  39.  
  40. PyApp()
  41. gtk.main()
Un ejemplo de lo quieres hacer.

Etiquetas: gtk, ventanas, formulario
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 09:00.