Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d3a52a

Browse files
committed
An example of a multi-threaded Tkinter program.
1 parent 1ad0071 commit 3d3a52a

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Demo/tkinter/guido/brownian.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# An example of a multi-threaded Tkinter program.
2+
3+
from Tkinter import *
4+
import random
5+
import threading
6+
import time
7+
import sys
8+
9+
WIDTH = 400
10+
HEIGHT = 300
11+
SIGMA = 10
12+
BUZZ = 2
13+
RADIUS = 2
14+
LAMBDA = 10
15+
FILL = 'red'
16+
17+
def particle(canvas):
18+
r = RADIUS
19+
x = random.gauss(WIDTH/2.0, SIGMA)
20+
y = random.gauss(HEIGHT/2.0, SIGMA)
21+
p = canvas.create_oval(x-r, y-r, x+r, y+r, fill=FILL)
22+
while 1:
23+
dx = random.gauss(0, BUZZ)
24+
dy = random.gauss(0, BUZZ)
25+
try:
26+
canvas.move(p, dx, dy)
27+
except TclError:
28+
break
29+
dt = random.expovariate(LAMBDA)
30+
time.sleep(dt)
31+
32+
def main():
33+
root = Tk()
34+
canvas = Canvas(root, width=WIDTH, height=HEIGHT)
35+
canvas.pack(fill='both', expand=1)
36+
np = 30
37+
if sys.argv[1:]:
38+
np = int(sys.argv[1])
39+
for i in range(np):
40+
t = threading.Thread(target=particle, args=(canvas,))
41+
t.start()
42+
root.mainloop()
43+
44+
main()

0 commit comments

Comments
 (0)