source: project/wiki/eggref/4/pstk @ 29978

Last change on this file since 29978 was 29978, checked in by svnwiki, 8 years ago

Anonymous wiki edit for IP [24.212.179.143]:

  • Property svnwiki:title set to PS/Tk
File size: 4.8 KB
Line 
1== PS/Tk
2
3PS/Tk provides an interface to the Tk toolkit, and is an effective tool for creating graphical interfaces. 
4A detailed [[http://homepages.stca.herts.ac.uk/~comqpcl/pstk/index.html|guide]] to using Tk from Scheme is in progress.
5
6[[toc:]]
7
8=== Examples
9
10Several examples can be found in the [[http://anonymous@code.call-cc.org/svn/chicken-eggs/release/4/pstk/examples/|svn repository]] for this egg.
11
12==== Hello World
13
14<enscript highlight=scheme>
15(require-extension pstk)
16
17(tk-start)
18
19(tk/pack
20  (tk 'create-widget 'button 'text: "Hello"
21      'command: (lambda () (display "Hello world") (newline)))
22  'padx: 20 'pady: 20)
23(tk-event-loop)
24</enscript>
25
26==== Simple Dialog
27
28<enscript highlight=scheme>
29(require-extension pstk)
30
31(define (celsius->fahrenheit item)
32  (let ((number (string->number item)))
33    (if (number? number)
34      (+ (* number 9/5) 32)
35      0.0)))
36
37(tk-start)
38(tk/wm 'title tk "Celsius to Fahrenheit")
39
40(let* ((celsius (tk 'create-widget 'entry))
41       (label (tk 'create-widget 'label))
42       (button (tk 'create-widget 'button
43                   'text: 'Calculate
44                   'command: (lambda ()
45                               (label 'configure
46                                      'text: (number->string (celsius->fahrenheit (celsius 'get))))))))
47  ; layout widgets in a grid
48  (tk/grid celsius 'column: 2 'row: 1 'sticky: 'we 'padx: 5 'pady: 5)
49  (tk/grid label 'column: 2 'row: 2 'sticky: 'we 'padx: 5 'pady: 5)
50  (tk/grid button 'column: 2 'row: 3 'sticky: 'we 'padx: 5 'pady: 5)
51  (tk/grid (tk 'create-widget 'label 'text: "celsius")
52           'column: 3 'row: 1 'sticky: 'w 'padx: 5 'pady: 5)
53  (tk/grid (tk 'create-widget 'label 'text: "is")
54           'column: 1 'row: 2 'sticky: 'e 'padx: 5 'pady: 5)
55  (tk/grid (tk 'create-widget 'label 'text: "fahrenheit")
56           'column: 3 'row: 2 'sticky: 'w 'padx: 5 'pady: 5)
57
58  (tk-event-loop))
59</enscript>
60
61=== Tips on Using PS/Tk
62
63* By default, the program {{tclsh8.5}} is called, but an alternative program may be provided as an optional argument to {{(tk-start)}}.  For a distributable application, you can bundle [[http://code.google.com/p/tclkit/|tclkit]] with your application, and call the tclkit application in {{tk-start}}.
64
65* Under windows, there is a problem with keyboard input.  Currently, the fix is to show a dialog box which is dismissed by pressing 'Enter' (not clicking!) directly after starting tk. 
66
67 (tk-start "wish85")
68 (tk/message-box 'title: "starting program" 'message: "Press ENTER" 'type: 'ok)
69 (tk-wm title tk "PROGRAM")
70 etc
71
72* It is helpful to put an exception handler around your tk code to prevent orphaned shells, especially when developing your program. e.g.
73<enscript>
74(handle-exceptions
75  exn
76  (tk-end)  ; make sure tk is closed in event of any error
77  ; begin program
78  (tk-start)
79 ; rest of  gui setup
80 )
81</enscript>
82
83* PS/Tk currently does not work on Windows 7 due some problem with Posix/process.  It will simply hang after (tk-start "tclsh85") See http://bugs.call-cc.org/ticket/765 for a workaround (You need to add
84<enscript>
85(set! process (lambda (#!rest rest)
86    (receive (a b c d) (apply process* rest)
87    (values a b c))))
88</enscript>
89
90to your .scm file like this:
91
92<enscript>
93(require-extension pstk)
94
95;; http://bugs.call-cc.org/ticket/765
96;; broken posix/process function under Windows 7 (MingW)
97(set! process (lambda (#!rest rest)
98    (receive (a b c d) (apply process* rest)
99    (values a b c))))
100(tk-start "tclsh85")
101(tk/pack
102  (tk 'create-widget 'button 'text: "Hello"
103      'command: (lambda () (display "Hello world") (newline)))
104  'padx: 20 'pady: 20)
105(tk-event-loop)
106</enscript>
107
108* If you want to display unicode characters then you need to add the following to pstk.scm:
109
110<enscript>
111     (tk-init-string
112       (string-intersperse
113                          '("package require Tk"
114                            "fconfigure stdin -encoding utf-8"
115                            "fconfigure stdout -encoding utf-8"
116                            "if {[package version tile] != \"\"} {"
117                            "    package require tile"
118                            "}"
119</enscript>
120
121Alternatively, add the following in your script:
122<enscript>
123(tk-start)
124(tk-eval "fconfigure stdin -encoding ascii")
125(tk-eval "fconfigure stdout -encoding ascii")
126</enscript>
127
128=== Authors
129
130Kenneth A Dickey, Nils M Holm and Wolf-Dieter Busch created the initial versions of pstk.  This
131port to Chicken, plus some additions, is by [[/users/peter-lane|Peter Lane]].
132
133=== License
134
135Public Domain.
136
137=== Requirements
138
139Requires an installation of [[http://www.tcl.tk/software/tcltk/|tcltk]] or [[http://code.google.com/p/tclkit/|tclkit]].
140
141=== Version History
142
143* version 1.2.2: get rid of implicit dependency on {{letrec*}}-semantics in {{letrec}} call (Peter Bex)
144* version 1.2.1: avoid hitting {{apply}} argument limit (Jim Ursetto)
145* version 1.2: removed posix dependency from meta file (felix)
146* version 1.1: working on windows as well as linux
147* version 1.0: first package.
Note: See TracBrowser for help on using the repository browser.