python开发_thread_布朗运动

时间:2023-03-09 20:14:42
python开发_thread_布朗运动

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

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

下面是运行效果:

python开发_thread_布朗运动

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

代码部分:

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

 # Brownian motion -- an example of a multi-threaded Tkinter program.

 from tkinter import *
import random
import threading
import time
import sys #画布大小
WIDTH = 400
HEIGHT = 300
SIGMA = 10
BUZZ = 2
RADIUS = 2
LAMBDA = 10
FILL = 'red' stop = 0 # Set when main loop exits def particle(canvas):
r = RADIUS
x = random.gauss(WIDTH/2.0, SIGMA)
y = random.gauss(HEIGHT/2.0, SIGMA)
p = canvas.create_oval(x-r, y-r, x+r, y+r, fill=FILL)
while not stop:
dx = random.gauss(0, BUZZ)
dy = random.gauss(0, BUZZ)
dt = random.expovariate(LAMBDA)
try:
canvas.move(p, dx, dy)
except TclError:
break
time.sleep(dt) def main():
global stop
root = Tk()
canvas = Canvas(root, width=WIDTH, height=HEIGHT)
canvas.pack(fill='both', expand=1)
#粒子数目
np = 30
if sys.argv[1:]:
np = int(sys.argv[1])
for i in range(np):
t = threading.Thread(target=particle, args=(canvas,))
t.start()
try:
root.mainloop()
finally:
stop = 1 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

python开发_thread_布朗运动

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

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

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