Simple! Y divertido! Introduje el uso de lambda, me pareció apropiado después de algunos ejemplos ya mostrados anteriormente.
Código fuente aquí y a continuación:
#!/usr/bin/env python# -*- coding: utf-8 -*-#
# Se importan las librerías requeridasimport pygtk
# Se revisa que la versión de pygtk sea la correctapygtk.require('2.0')
# Se importa la librería gtkimport gtk
# Se crea la clase "Base" para utilizarla en la aplicaciónclass Base:# Se define la función "crea_ventana" para trabajar sobre esta funcióndef crea_ventana(self):# Se crea la ventana principal del nivel superiorwinpri = gtk.Window(gtk.WINDOW_TOPLEVEL)# Se establece el título de la ventanawinpri.set_title("Manejo de etiquetas")# Se establece el tamaño de la ventanawinpri.set_size_request(250, 250)# Se posiciona la ventana en la pantallawinpri.move(500, 300)# Se establece la propiedad de "no cambiar tamaño"winpri.set_resizable(False)# Se conecta la señal "delete_event" a la función que llamará a gtk.main_quit# usando "lambda" para generar en la misma línea la funciónwinpri.connect("delete_event", lambda x,y: gtk.main_quit())# ------------------- Comienza la creación de controles ------------------------lb1 = gtk.Label("Linea 01")lb2 = gtk.Label("Linea 02")lb3 = gtk.Label("Linea 03")lb4 = gtk.Label("Linea 04")lb5 = gtk.Label("Linea 05")lb6 = gtk.Label("Linea 06")lb7 = gtk.Label("Linea 07")lb8 = gtk.Label("Linea 08")lb9 = gtk.Label("Linea 09")lb10 = gtk.Label("Linea 10")bt1 = gtk.Button("+")bt2 = gtk.Button("+")bt3 = gtk.Button("+")bt4 = gtk.Button("+")bt5 = gtk.Button("+")bt6 = gtk.Button("+")bt7 = gtk.Button("+")bt8 = gtk.Button("+")bt9 = gtk.Button("+")bt10 = gtk.Button("+")bt11 = gtk.Button("-")bt12 = gtk.Button("-")bt13 = gtk.Button("-")bt14 = gtk.Button("-")bt15 = gtk.Button("-")bt16 = gtk.Button("-")bt17 = gtk.Button("-")bt18 = gtk.Button("-")bt19 = gtk.Button("-")bt20 = gtk.Button("-")hb1 = gtk.HBox(True, 5)hb2 = gtk.HBox(True, 5)hb3 = gtk.HBox(True, 5)hb4 = gtk.HBox(True, 5)hb5 = gtk.HBox(True, 5)hb6 = gtk.HBox(True, 5)hb7 = gtk.HBox(True, 5)hb8 = gtk.HBox(True, 5)hb9 = gtk.HBox(True, 5)hb10 = gtk.HBox(True, 5)vb = gtk.VBox(True, 0)# ------------------- Termina la creación de controles -------------------------# ------------------- Se conectan las señales a los controles ------------------bt1.connect("clicked", lambda s: lb1.set_text("Linea 01"))bt2.connect("clicked", lambda s: lb2.set_text("Linea 02"))bt3.connect("clicked", lambda s: lb3.set_text("Linea 03"))bt4.connect("clicked", lambda s: lb4.set_text("Linea 04"))bt5.connect("clicked", lambda s: lb5.set_text("Linea 05"))bt6.connect("clicked", lambda s: lb6.set_text("Linea 06"))bt7.connect("clicked", lambda s: lb7.set_text("Linea 07"))bt8.connect("clicked", lambda s: lb8.set_text("Linea 08"))bt9.connect("clicked", lambda s: lb9.set_text("Linea 09"))bt10.connect("clicked", lambda s: lb10.set_text("Linea 10"))bt11.connect("clicked", lambda s: lb1.set_text(" "))bt12.connect("clicked", lambda s: lb2.set_text(" "))bt13.connect("clicked", lambda s: lb3.set_text(" "))bt14.connect("clicked", lambda s: lb4.set_text(" "))bt15.connect("clicked", lambda s: lb5.set_text(" "))bt16.connect("clicked", lambda s: lb6.set_text(" "))bt17.connect("clicked", lambda s: lb7.set_text(" "))bt18.connect("clicked", lambda s: lb8.set_text(" "))bt19.connect("clicked", lambda s: lb9.set_text(" "))bt20.connect("clicked", lambda s: lb10.set_text(" "))# ------------------- Termina de conectar señales a los controles ------------# Se añade a la ventana principal la caja vertical que contiene a los demás contenedoreswinpri.add(vb)# ------------------- Comienza el empaquetado de controles -------------------hb1.pack_start(lb1, False, True, 0)hb1.pack_start(bt1, False, True, 0)hb1.pack_start(bt11, False, True, 0)vb.pack_start(hb1, True, True, 0)hb2.pack_start(lb2, True, True, 0)hb2.pack_start(bt2, True, True, 0)hb2.pack_start(bt12, True, True, 0)vb.pack_start(hb2, True, True, 0)hb3.pack_start(lb3, True, True, 0)hb3.pack_start(bt3, True, True, 0)hb3.pack_start(bt13, True, True, 0)vb.pack_start(hb3, True, True, 0)hb4.pack_start(lb4, True, True, 0)hb4.pack_start(bt4, True, True, 0)hb4.pack_start(bt14, True, True, 0)vb.pack_start(hb4, True, True, 0)hb5.pack_start(lb5, True, True, 0)hb5.pack_start(bt5, True, True, 0)hb5.pack_start(bt15, True, True, 0)vb.pack_start(hb5, True, True, 0)hb6.pack_start(lb6, True, True, 0)hb6.pack_start(bt6, True, True, 0)hb6.pack_start(bt16, True, True, 0)vb.pack_start(hb6, True, True, 0)hb7.pack_start(lb7, True, True, 0)hb7.pack_start(bt7, True, True, 0)hb7.pack_start(bt17, True, True, 0)vb.pack_start(hb7, True, True, 0)hb8.pack_start(lb8, True, True, 0)hb8.pack_start(bt8, True, True, 0)hb8.pack_start(bt18, True, True, 0)vb.pack_start(hb8, True, True, 0)hb9.pack_start(lb9, True, True, 0)hb9.pack_start(bt9, True, True, 0)hb9.pack_start(bt19, True, True, 0)vb.pack_start(hb9, True, True, 0)hb10.pack_start(lb10, True, True, 0)hb10.pack_start(bt10, True, True, 0)hb10.pack_start(bt20, True, True, 0)vb.pack_start(hb10, True, True, 0)# -------------------Termina el empaquetado de controles ---------------------# Se muestran TODOS los controleswinpri.show_all()# Se define la función inicial para llamar a la función principaldef __init__(self):self.crea_ventana()# Se define la función que llamará al ciclo principal de gtkdef main(self):gtk.main()if __name__ == "__main__":base = Base()base.main()
No hay comentarios.:
Publicar un comentario