python開發之thread實現布朗運動的方法

來源:互聯網
上載者:User
本文執行個體講述了python開發之thread實現布朗運動的方法。分享給大家供大家參考,具體如下:

這裡我將給大家介紹有關python中thread來實現布朗運動的一個例子

下面是運行效果:

代碼部分:

# Brownian motion -- an example of a multi-threaded Tkinter program.from tkinter import *import randomimport threadingimport timeimport sys#畫布大小WIDTH = 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()

希望本文所述對大家Python程式設計有所協助。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.