blob: ec3dbef8c57fc69c5982628b11e0b1af434fd78c [file] [log] [blame]
Guido van Rossum3d3a52a1998-05-28 22:52:01 +00001# 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
17def particle(canvas):
18 r = RADIUS
19 x = random.gauss(WIDTH/2.0, SIGMA)
20 y = random.gauss(HEIGHT/2.0, SIGMA)
21 p = canvas.create_oval(x-r, y-r, x+r, y+r, fill=FILL)
22 while 1:
23 dx = random.gauss(0, BUZZ)
24 dy = random.gauss(0, BUZZ)
25 try:
26 canvas.move(p, dx, dy)
27 except TclError:
28 break
29 dt = random.expovariate(LAMBDA)
30 time.sleep(dt)
31
32def main():
33 root = Tk()
34 canvas = Canvas(root, width=WIDTH, height=HEIGHT)
35 canvas.pack(fill='both', expand=1)
36 np = 30
37 if sys.argv[1:]:
38 np = int(sys.argv[1])
39 for i in range(np):
40 t = threading.Thread(target=particle, args=(canvas,))
41 t.start()
42 root.mainloop()
43
44main()