source: python/trunk/Demo/tkinter/guido/brownian.py

Last change on this file was 2, checked in by Yuri Dario, 15 years ago

Initial import for vendor code.

  • Property svn:eol-style set to native
File size: 1.1 KB
Line 
1# Brownian motion -- an example of a multi-threaded Tkinter program.
2
3from Tkinter import *
4import random
5import threading
6import time
7import sys
8
9WIDTH = 400
10HEIGHT = 300
11SIGMA = 10
12BUZZ = 2
13RADIUS = 2
14LAMBDA = 10
15FILL = 'red'
16
17stop = 0 # Set when main loop exits
18
19def particle(canvas):
20 r = RADIUS
21 x = random.gauss(WIDTH/2.0, SIGMA)
22 y = random.gauss(HEIGHT/2.0, SIGMA)
23 p = canvas.create_oval(x-r, y-r, x+r, y+r, fill=FILL)
24 while not stop:
25 dx = random.gauss(0, BUZZ)
26 dy = random.gauss(0, BUZZ)
27 dt = random.expovariate(LAMBDA)
28 try:
29 canvas.move(p, dx, dy)
30 except TclError:
31 break
32 time.sleep(dt)
33
34def main():
35 global stop
36 root = Tk()
37 canvas = Canvas(root, width=WIDTH, height=HEIGHT)
38 canvas.pack(fill='both', expand=1)
39 np = 30
40 if sys.argv[1:]:
41 np = int(sys.argv[1])
42 for i in range(np):
43 t = threading.Thread(target=particle, args=(canvas,))
44 t.start()
45 try:
46 root.mainloop()
47 finally:
48 stop = 1
49
50main()
Note: See TracBrowser for help on using the repository browser.