| Line | |
|---|
| 1 | # Brownian motion -- an example of a multi-threaded Tkinter program.
|
|---|
| 2 |
|
|---|
| 3 | from Tkinter import *
|
|---|
| 4 | import random
|
|---|
| 5 | import threading
|
|---|
| 6 | import time
|
|---|
| 7 | import sys
|
|---|
| 8 |
|
|---|
| 9 | WIDTH = 400
|
|---|
| 10 | HEIGHT = 300
|
|---|
| 11 | SIGMA = 10
|
|---|
| 12 | BUZZ = 2
|
|---|
| 13 | RADIUS = 2
|
|---|
| 14 | LAMBDA = 10
|
|---|
| 15 | FILL = 'red'
|
|---|
| 16 |
|
|---|
| 17 | stop = 0 # Set when main loop exits
|
|---|
| 18 |
|
|---|
| 19 | def 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 |
|
|---|
| 34 | def 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 |
|
|---|
| 50 | main()
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.