11# Display digits of pi in a window, calculating in a separate thread.
22# Compare ../scripts/pi.py.
33
4+ import sys
45import time
56import thread
67import stdwin
78from stdwinevents import *
89
10+ ok = 1
11+
912digits = []
1013
1114def worker ():
1215 k , a , b , a1 , b1 = 2l , 4l , 1l , 12l , 4l
13- while 1 :
16+ while ok :
1417 # Next approximation
1518 p , q , k = k * k , 2l * k + 1l , k + 1l
1619 a , b , a1 , b1 = a1 , b1 , p * a + q * a1 , p * b + q * b1
@@ -23,11 +26,13 @@ def worker():
2326 d , d1 = a / b , a1 / b1
2427
2528def main ():
29+ global ok
2630 digits_seen = 0
2731 thread .start_new_thread (worker , ())
2832 tw = stdwin .textwidth ('0 ' )
2933 lh = stdwin .lineheight ()
3034 stdwin .setdefwinsize (20 * tw , 20 * lh )
35+ stdwin .setdefscrollbars (0 , 1 )
3136 win = stdwin .open ('digits of pi' )
3237 options = win .menucreate ('Options' )
3338 options .additem ('Auto scroll' )
@@ -37,7 +42,8 @@ def main():
3742 win .settimer (1 )
3843 type , w , detail = stdwin .getevent ()
3944 if type == WE_CLOSE :
40- thread .exit_prog (0 )
45+ ok = 0
46+ sys .exit (0 )
4147 elif type == WE_DRAW :
4248 (left , top ), (right , bottom ) = detail
4349 digits_seen = len (digits )
0 commit comments