python开发之thread实现布朗运动的方法
作者:Hongten 时间:2021-04-10 00:17:36
本文实例讲述了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()
希望本文所述对大家Python程序设计有所帮助。
标签:python,thread,运动
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 19个值得学习的编程技巧
2021-09-21 22:42:30
PHP程序员玩转Linux系列 备份还原MySQL
2023-11-15 07:39:28
![](https://img.aspxhome.com/file/2023/7/67207_0s.png)
网页常用特效整理:初级篇
2023-06-27 00:58:37
sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查询计划】
2011-09-30 11:09:37
Pytorch 实现自定义参数层的例子
2023-01-27 22:00:06
Python第三方包之DingDingBot钉钉机器人
2022-09-06 14:50:38
提高网页加载显示速度的方法
2007-08-10 13:17:00
Web2.0电子商务网站的交互设计
2009-05-15 12:08:00
![](https://img.aspxhome.com/file/UploadPic/20095/15/01-80s.jpg)
彻底弄懂CSS盒子模式之二(导航栏实例)
2007-05-11 16:52:00
![](https://img.aspxhome.com/file/uploadpic/20075/2007511165235848.jpg)
Response.Flush的使用心得
2010-04-08 12:57:00
Python协程原理全面分析
2022-10-02 01:43:43
python实现微信自动回复机器人功能
2023-12-30 01:01:40
![](https://img.aspxhome.com/file/2023/8/64718_0s.jpg)
Python 如何读取字典的所有键-值对
2021-05-21 10:06:53
PHP商品秒杀问题解决方案实例详解【mysql与redis】
2023-09-27 10:49:04
python使用pip安装SciPy、SymPy、matplotlib教程
2022-03-05 01:46:12
asp实现本周的一周时间列表的代码
2011-04-06 10:45:00
Python通过队列实现进程间通信详情
2023-07-24 17:49:44
![](https://img.aspxhome.com/file/2023/9/68939_0s.png)
小谈MySQL字符集
2009-02-13 13:30:00
python爬虫要用到的库总结
2021-04-16 23:55:40
PHP基于phpqrcode类库生成二维码过程解析
2023-11-17 19:06:35