blob: 97dad2d7336f25aaa15d658500fed802174d5ecf [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum4450bab1994-10-11 14:57:03 +00002
Guido van Rossum50df3811994-06-28 13:52:31 +00003# Simulate "electrons" migrating across the screen.
4# An optional bitmap file in can be in the background.
5#
6# Usage: electrons [n [bitmapfile]]
7#
Guido van Rossumf638d1c1998-05-26 21:43:44 +00008# n is the number of electrons to animate; default is 30.
Guido van Rossum50df3811994-06-28 13:52:31 +00009#
10# The bitmap file can be any X11 bitmap file (look in
11# /usr/include/X11/bitmaps for samples); it is displayed as the
12# background of the animation. Default is no bitmap.
13
Guido van Rossum50df3811994-06-28 13:52:31 +000014from Tkinter import *
Guido van Rossumf638d1c1998-05-26 21:43:44 +000015import random
Guido van Rossum50df3811994-06-28 13:52:31 +000016
17
Guido van Rossum50df3811994-06-28 13:52:31 +000018# 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,
Guido van Rossum89cb67b1996-07-30 18:57:18 +000032 bitmap=bitmap,
33 foreground='blue')
Guido van Rossum50df3811994-06-28 13:52:31 +000034
Guido van Rossumf638d1c1998-05-26 21:43:44 +000035 self.pieces = []
Guido van Rossum50df3811994-06-28 13:52:31 +000036 x1, y1, x2, y2 = 10,70,14,74
Guido van Rossumf638d1c1998-05-26 21:43:44 +000037 for i in range(n):
Guido van Rossum89cb67b1996-07-30 18:57:18 +000038 p = c.create_oval(x1, y1, x2, y2, fill='red')
Guido van Rossumf638d1c1998-05-26 21:43:44 +000039 self.pieces.append(p)
Guido van Rossum50df3811994-06-28 13:52:31 +000040 y1, y2 = y1 +2, y2 + 2
41 self.tk.update()
42
Guido van Rossumf638d1c1998-05-26 21:43:44 +000043 def random_move(self, n):
Guido van Rossum29892d81998-05-19 21:16:10 +000044 c = self.canvas
Guido van Rossumf638d1c1998-05-26 21:43:44 +000045 for p in self.pieces:
Guido van Rossum6c3a2cb1998-05-20 17:13:01 +000046 x = random.choice(range(-2,4))
47 y = random.choice(range(-3,4))
Guido van Rossum50df3811994-06-28 13:52:31 +000048 c.move(p, x, y)
49 self.tk.update()
Guido van Rossum89cb67b1996-07-30 18:57:18 +000050
Guido van Rossumf638d1c1998-05-26 21:43:44 +000051 # Run -- allow 500 movemens
Guido van Rossum50df3811994-06-28 13:52:31 +000052 def run(self):
Guido van Rossumdac44471997-08-14 19:49:27 +000053 try:
Guido van Rossum29892d81998-05-19 21:16:10 +000054 for i in range(500):
Guido van Rossumdac44471997-08-14 19:49:27 +000055 self.random_move(self.n)
Guido van Rossumbd8341e1998-04-10 19:17:41 +000056 except TclError:
57 try:
58 self.tk.destroy()
59 except TclError:
60 pass
Guido van Rossum89cb67b1996-07-30 18:57:18 +000061
Guido van Rossum50df3811994-06-28 13:52:31 +000062
63# Main program
64def main():
65 import sys, string
66
Guido van Rossumf638d1c1998-05-26 21:43:44 +000067 # First argument is number of electrons, default 30
Guido van Rossum50df3811994-06-28 13:52:31 +000068 if sys.argv[1:]:
69 n = string.atoi(sys.argv[1])
70 else:
71 n = 30
72
73 # Second argument is bitmap file, default none
74 if sys.argv[2:]:
75 bitmap = sys.argv[2]
76 # Reverse meaning of leading '@' compared to Tk
77 if bitmap[0] == '@': bitmap = bitmap[1:]
78 else: bitmap = '@' + bitmap
79 else:
80 bitmap = None
81
82 # Create the graphical objects...
83 h = Electrons(n, bitmap)
84
85 # ...and run!
86 h.run()
87
88
89# Call main when run as script
90if __name__ == '__main__':
91 main()