PyGTK

PyGTK
Información general
Tipo de programa Widget toolkit
Desarrollador PyGTK Core development team
Licencia LGPL
Información técnica
Programado en Python
Versiones
Última versión estable 2.24.0 (01 de abril de 2011 (15 años, 2 meses y 6 días))
Lanzamientos
PyGTK
PyGObject
Enlaces

PyGTK es un binding de la biblioteca gráfica GTK para el lenguaje de programación Python. La biblioteca GTK se usa para desarrollar el entorno gráfico GNOME, así como sus aplicaciones, a la vez que algunos otros entornos gráficos. La biblioteca GTK permite el desarrollo sencillo de interfaces gráficas y su uso conjunto con Python permite el desarrollo rápido de aplicaciones gráficas potentes.

Ejemplos

A continuación se muestran algunos ejemplos del uso de PyGTK

Hola Mundo

El código siguiente producirá una ventana de 200x200 píxeles con las palabras "Hola mundo" en el interior.

import gtk

def crear_ventana():
    ventana = gtk.Window()
    ventana.set_default_size(200, 200)
    ventana.connect('destroy', gtk.main_quit)

    etiqueta = gtk.Label('Hola mundo')
    ventana.add(etiqueta)

    etiqueta.show()
    ventana.show()

crear_ventana()
gtk.main()

Para que el script se pueda ejecutar en un sistema *nix, la primera línea debe ser "#!/usr/bin/env python" o "#!/usr/bin/python" para inidicar donde se encuentra el intérprete. Además, el script debe disponer de permisos de ejecución.

Juego del 15

El siguiente programa es una implementación del juego del 15 que consiste en una ventana y botones que cambiarán su posición conforme se opriman.

import gtk
from random import shuffle

def callback(boton):
    (x, y), (bx, by) = boton.posicion, blanco.posicion
    if (x-bx, y-by) in [(-1,0), (1,0), (0,1), (0,-1)]:
        tabla.remove(boton)
        tabla.remove(blanco)
        tabla.attach(boton, bx, bx+1, by, by+1)
        tabla.attach(blanco, x, x+1, y, y+1)
        boton.posicion = (bx, by)
        blanco.posicion = (x, y)

tam     = 4
ventana = gtk.Window()
ventana.set_default_size(500, 500)
botones = [gtk.Button(str(i)) for i in range(1, tam * tam)]
blanco  = gtk.Button()
tabla   = gtk.Table(tam, tam, homogeneous=True)

shuffle (botones)

(x, y) = (0, 0)
for i in botones + [blanco]:
    if x == tam: x = 0; y += 1
    i.connect('clicked', callback)
    i.posicion = (x, y)
    tabla.attach(i, x, x+1, y, y+1)
    x += 1

ventana.connect('destroy', gtk.main_quit)
ventana.add(tabla)
ventana.set_title('Juego del '+str(tam*tam-1))
ventana.show_all()
blanco.hide()
gtk.main()

Note que es posible variar la cantidad de botones de la ventana cambiando el valor de 'tam'. El número de botones será 'tam*tam-1'. Para poder ejecutar éste script, se deberán seguir los pasos mencionados en el ejemplo anterior.

Aplicaciones que usan PyGTK

PyGTK se utiliza en algunos proyectos relevantes, por ejemplo:

Véase también

Enlaces externos

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.