blob: 56f33f46a2c6aee0a6bb379c9210a9fef8e08f1b [file] [log] [blame]
Guido van Rossum50df3811994-06-28 13:52:31 +00001#!/ufs/guido/bin/sgi/tkpython
2# Simulate "electrons" migrating across the screen.
3# An optional bitmap file in can be in the background.
4#
5# Usage: electrons [n [bitmapfile]]
6#
7# n is the number of electrons to animate; default is 4, maximum 15.
8#
9# The bitmap file can be any X11 bitmap file (look in
10# /usr/include/X11/bitmaps for samples); it is displayed as the
11# background of the animation. Default is no bitmap.
12
13# This uses Steen Lumholt's Tk interface
14from Tkinter import *
15
16
17
18# The graphical interface
19class Electrons:
20
21 # Create our objects
22 def __init__(self, n, bitmap = None):
23 self.n = n
24 self.tk = tk = Tk()
25 self.canvas = c = Canvas(tk)
26 c.pack()
27 width, height = tk.getint(c['width']), tk.getint(c['height'])
28
29 # Add background bitmap
30 if bitmap:
31 self.bitmap = c.create_bitmap(width/2, height/2,
32 {'bitmap': bitmap,
33 'foreground': 'blue'})
34
35 self.pieces = {}
36 x1, y1, x2, y2 = 10,70,14,74
37 for i in range(n,0,-1):
38 p = c.create_oval(x1, y1, x2, y2,
39 {'fill': 'red'})
40 self.pieces[i] = p
41 y1, y2 = y1 +2, y2 + 2
42 self.tk.update()
43
44 def random_move(self,n):
45 for i in range(1,n+1):
46 p = self.pieces[i]
47 c = self.canvas
48 import rand
49 x = rand.choice(range(-2,4))
50 y = rand.choice(range(-3,4))
51 c.move(p, x, y)
52 self.tk.update()
53 # Run -- never returns
54 def run(self):
55 while 1:
56 self.random_move(self.n)
57 self.tk.mainloop() # Hang around...
58
59# Main program
60def main():
61 import sys, string
62
63 # First argument is number of pegs, default 4
64 if sys.argv[1:]:
65 n = string.atoi(sys.argv[1])
66 else:
67 n = 30
68
69 # Second argument is bitmap file, default none
70 if sys.argv[2:]:
71 bitmap = sys.argv[2]
72 # Reverse meaning of leading '@' compared to Tk
73 if bitmap[0] == '@': bitmap = bitmap[1:]
74 else: bitmap = '@' + bitmap
75 else:
76 bitmap = None
77
78 # Create the graphical objects...
79 h = Electrons(n, bitmap)
80
81 # ...and run!
82 h.run()
83
84
85# Call main when run as script
86if __name__ == '__main__':
87 main()
88
89