python开发_thread_布朗运动

这篇blog是非常有趣的,是的,他非常有趣

下面我将给大家介绍有关python中thread来实现布朗运动的一个例子

下面是运行效果:

布朗运行

===================================================

代码部分:

===================================================

 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 #画布大小
10 WIDTH = 400
11 HEIGHT = 300
12 SIGMA = 10
13 BUZZ = 2
14 RADIUS = 2
15 LAMBDA = 10
16 FILL = 'red'
17 
18 stop = 0                                # Set when main loop exits
19 
20 def particle(canvas):
21     r = RADIUS
22     x = random.gauss(WIDTH/2.0, SIGMA)
23     y = random.gauss(HEIGHT/2.0, SIGMA)
24     p = canvas.create_oval(x-r, y-r, x+r, y+r, fill=FILL)
25     while not stop:
26         dx = random.gauss(0, BUZZ)
27         dy = random.gauss(0, BUZZ)
28         dt = random.expovariate(LAMBDA)
29         try:
30             canvas.move(p, dx, dy)
31         except TclError:
32             break
33         time.sleep(dt)
34 
35 def main():
36     global stop
37     root = Tk()
38     canvas = Canvas(root, width=WIDTH, height=HEIGHT)
39     canvas.pack(fill='both', expand=1)
40     #粒子数目
41     np = 30
42     if sys.argv[1:]:
43         np = int(sys.argv[1])
44     for i in range(np):
45         t = threading.Thread(target=particle, args=(canvas,))
46         t.start()
47     try:
48         root.mainloop()
49     finally:
50         stop = 1
51 
52 main()

更多资料:http://www.oschina.net/code/explore/Python-3.1.3/Demo/tkinter/guido/brownian.py

 

========================================================

More reading,and english is important.

I'm Hongten

 

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

========================================================

posted @ 2013-10-03 15:55  Hongten  阅读(3280)  评论(0编辑  收藏  举报
Fork me on GitHub