#lang scheme (require scheme/foreign) (unsafe!) #| Convert nested strings to a string of tab indented lines |# (define (sexp->tab-lines s (indent "")) (if (list? s) (apply string-append (map (lambda (s) (sexp->tab-lines s (string-append indent "\t"))) s)) (string-append indent s "\n"))) ; Dynamically load the python shared library (version 2.6.1 on my system) (define py (ffi-lib "libpython2.6" "1")) ; ; Get a function from it, of type string -> int, mirrored as a scheme function. (define py-run-simple-string (get-ffi-obj "PyRun_SimpleString" py (_fun _string -> _int))) ; ; Get, and immediately call, Py_Initialize to start python. ((get-ffi-obj "Py_Initialize" py (_fun -> _void))) ; Run a pygtk Hello World. ; - blocks the GUI, so close the pygtk window to get DrScheme control back. (for-each py-run-simple-string (map sexp->tab-lines '("import pygtk" "import gtk" ("class HelloWorld:" ("def delete_event(self, widget, event, data=None):" ("return False")) ("def destroy(self, widget, data=None):" ("gtk.main_quit()")) ("def __init__(self):" ("self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)" "self.window.connect(\"delete_event\", self.delete_event)" "self.window.connect(\"destroy\", self.destroy)" "self.window.show()")) ("def main(self):" ("gtk.main()"))) "hello = HelloWorld()" "hello.main()"))) #| Some informative noodling (define py-init (get-ffi-obj "Py_Initialize" py (_cprocedure '() _void))) ((get-ffi-obj "Py_IsInitialized" py (_cprocedure '() _int))) (define py-init? (get-ffi-obj "Py_IsInitialized" py (_cprocedure '() _bool))) (py-init?) (py-run-simple-string "print \"csc150\"") ; seen, e.g., in terminal if drscheme launched from there (for-each (compose display py-run-simple-string)) |#