⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 brownian.py

📁 reduced python source for embedded apps
💻 PY
字号:
# Brownian motion -- an example of a multi-threaded Tkinter program.from Tkinter import *import randomimport threadingimport timeimport sysWIDTH = 400HEIGHT = 300SIGMA = 10BUZZ = 2RADIUS = 2LAMBDA = 10FILL = 'red'stop = 0                                # Set when main loop exitsdef 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 = 1main()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -