这篇blog是非常有趣的,是的,他非常有趣
下面我将给大家介绍有关python中thread来实现布朗运动的一个例子
下面是运行效果:
===================================================
代码部分:
===================================================
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
811 # 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
复制代码
1大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。
E | hongtenzone@foxmail.com B | http://www.cnblogs.com/hongten
========================================================
最后
以上就是细心白猫最近收集整理的关于python开发_thread_布朗运动的全部内容,更多相关python开发_thread_布朗运动内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复