In this paper, we describe the method of developing the thread of Python to implement Brownian motion. Share to everyone for your reference, as follows:
Here I will give you an example of the thread in Python to implement Brownian motion.
Here's how it works:
Code section:
# Brownian Motion--an example of a multi-threaded Tkinter program.from Tkinter import *import randomimport threadingimpo RT timeimport sys# Canvas Size width = 400HEIGHT = 300SIGMA = 10BUZZ = 2RADIUS = 2LAMBDA = 10FILL = ' Red ' stop = 0 # Set when main l OOP 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) D y = 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 = 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 ()
I hope this article is helpful for Python program design.