Jelajahi Sumber

New pygtk example

Thomas Kluyver 11 tahun lalu
induk
melakukan
9903e48dce

+ 21 - 0
examples/pygtk/README.rst

@@ -0,0 +1,21 @@
+This is an example of building a Windows installer for a pygtk application. This
+is a bit more complex than the other examples, because the GTK runtime needs to
+be set up. This needs two things:
+
+1. The pieces of the GTK runtime and its Python bindings. The script ``grab_files.sh``
+   downloads these, unpacks them, trims out unnecessary pieces, and places them
+   where pynsist will find them.
+2. The ``PATH`` environment variable must be modified before we try to import
+   the Python GTK bindings. This is done by the ``extra_preamble`` field in
+   ``installer.cfg``.
+
+I referred to the following sources of information to work this out:
+
+Bundling pygtk using py2exe:
+http://faq.pygtk.org/index.py?file=faq21.005.htp&req=show
+https://web.archive.org/web/20060208162511/http://www.anti-particle.com/py2exe-0.5.shtml
+
+Installing pygtk & deps: http://www.pygtk.org/downloads.html
+(inc links for pygtk, pycairo and pygobject installers)
+
+GTK bundles for Windows: http://www.gtk.org/download/win32.php

+ 40 - 0
examples/pygtk/grab_files.sh

@@ -0,0 +1,40 @@
+# Download the necessary files
+wget -O gtkbundle.zip http://ftp.gnome.org/pub/gnome/binaries/win32/gtk+/2.24/gtk+-bundle_2.24.10-20120208_win32.zip
+wget -O pygobject.exe http://ftp.gnome.org/pub/GNOME/binaries/win32/pygobject/2.28/pygobject-2.28.3.win32-py2.7.exe
+wget -O pycairo.exe http://ftp.gnome.org/pub/GNOME/binaries/win32/pycairo/1.8/pycairo-1.8.10.win32-py2.7.exe
+wget -O pygtk.exe http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.24/pygtk-2.24.0.win32-py2.7.exe
+
+# GTK runtime
+mkdir gtkbundle
+unzip -d gtkbundle gtkbundle.zip
+cd gtkbundle
+rm -r src man include share/doc share/man share/gtk-doc share/gtk-2.0/demo bin/gtk-demo.exe etc/bash_completion.d
+cd ..
+
+# Python bindings
+mkdir pygobject
+unzip -d pygobject pygobject.exe
+mkdir pycairo
+unzip -d pycairo pycairo.exe
+mkdir pygtk
+unzip -d pygtk pygtk.exe
+
+# Reassemble into pynsist_pkgs
+echo -n "Assembling GTK files into pynsist_pkgs... "
+rm -r pynsist_pkgs
+mkdir pynsist_pkgs
+mv gtkbundle pynsist_pkgs/gtk
+
+cp -r pygobject/PLATLIB/* pynsist_pkgs
+rm -r pygobject
+
+cp -r pycairo/PLATLIB/* pynsist_pkgs
+cp -r pycairo/DATA/lib/site-packages/cairo/* pynsist_pkgs/cairo
+rm -r pycairo
+
+cp -r pygtk/PLATLIB/* pynsist_pkgs
+rm -r pygtk
+
+rm -r pynsist_pkgs/gtk-2.0/tests
+
+echo "done"

+ 84 - 0
examples/pygtk/helloworld.py

@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+
+# This example was adapted from http://pygtk.org/pygtk2tutorial/examples/helloworld.py
+
+import pygtk
+pygtk.require('2.0')
+import gtk
+
+class HelloWorld:
+    def msgbox(self, text):
+        "Display a simple message box"
+        md = gtk.MessageDialog(self.window, gtk.DIALOG_DESTROY_WITH_PARENT,
+                          gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, text)
+        md.run()
+
+    # This is a callback function. The data arguments are ignored
+    # in this example. More on callbacks below.
+    def hello(self, widget, data=None):
+        self.msgbox("Hello, world!")
+
+    def delete_event(self, widget, event, data=None):
+        # If you return FALSE in the "delete_event" signal handler,
+        # GTK will emit the "destroy" signal. Returning TRUE means
+        # you don't want the window to be destroyed.
+        # This is useful for popping up 'are you sure you want to quit?'
+        # type dialogs.
+        self.msgbox("delete event occurred")
+
+        # Change FALSE to TRUE and the main window will not be destroyed
+        # with a "delete_event".
+        return False
+
+    def destroy(self, widget, data=None):
+        gtk.main_quit()
+
+    def __init__(self):
+        # create a new window
+        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
+    
+        # When the window is given the "delete_event" signal (this is given
+        # by the window manager, usually by the "close" option, or on the
+        # titlebar), we ask it to call the delete_event () function
+        # as defined above. The data passed to the callback
+        # function is NULL and is ignored in the callback function.
+        self.window.connect("delete_event", self.delete_event)
+    
+        # Here we connect the "destroy" event to a signal handler.  
+        # This event occurs when we call gtk_widget_destroy() on the window,
+        # or if we return FALSE in the "delete_event" callback.
+        self.window.connect("destroy", self.destroy)
+    
+        # Sets the border width of the window.
+        self.window.set_border_width(10)
+    
+        # Creates a new button with the label "Hello World".
+        self.button = gtk.Button("Hello World")
+    
+        # When the button receives the "clicked" signal, it will call the
+        # function hello() passing it None as its argument.  The hello()
+        # function is defined above.
+        self.button.connect("clicked", self.hello, None)
+    
+        # This will cause the window to be destroyed by calling
+        # gtk_widget_destroy(window) when "clicked".  Again, the destroy
+        # signal could come from here, or the window manager.
+        self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
+    
+        # This packs the button into the window (a GTK container).
+        self.window.add(self.button)
+    
+        # The final step is to display this newly created widget.
+        self.button.show()
+    
+        # and the window
+        self.window.show()
+
+    def main(self):
+        # All PyGTK applications must have a gtk.main(). Control ends here
+        # and waits for an event to occur (like a key press or mouse event).
+        gtk.main()
+
+def main():
+    hello = HelloWorld()
+    hello.main()

+ 12 - 0
examples/pygtk/installer.cfg

@@ -0,0 +1,12 @@
+[Application]
+name=Hello World (PyGTK)
+version=1.0
+entry_point=helloworld:main
+extra_preamble=os.environ['PATH'] += os.pathsep + os.path.join(pkgdir, 'gtk/lib') + \
+    os.pathsep + os.path.join(pkgdir, 'gtk/bin')
+
+[Python]
+version=2.7.7
+
+[Include]
+packages=pygtk