# Copyright (c) 1996 CNRI. # This improves over Java's Blink demo by having a font parameter. from Tkinter import * import string from whrandom import random # I hate blinking things. # --Guido van Rossum FONT = "-*-times-medium-r-normal-*-*-180-*-*-*-*-*-*" LBL = "The quick brown fox jumps over the lazy dog" class Blink: def __init__(self, master, width=0, height=0, lbl=LBL, speed=2.5, font=FONT): self.speed = int(1000/speed) # (Hack for b/w compatibility) if width and height: # Old Grail tag: width, height passed to widget self.frame = Frame(master, width=width, height=height) self.frame.pack() else: # New Grail tag: width, height set parent size self.frame = master width = string.atoi(master['width']) height = string.atoi(master['height']) # Get some font metrics by measuring a dummy widget label = Label(self.frame, text=" ", font=font, padx=0, pady=0) space, line = label.winfo_reqwidth(), label.winfo_reqheight() label.destroy() x, y = 0, 0 self.labels = [] for word in string.split(lbl): label = Label(self.frame, text=word, font=font, padx=0, pady=0) w, h = label.winfo_reqwidth() + space, label.winfo_reqheight() if x + w > width: x, y = 0, y + line label.place(x=x, y=y) self.labels.append(label) x = x + w self.blinker() def blinker(self): # Hack to test if we still exist try: self.frame['width'] except: return red = int(random()*50) green = int(random()*50) blue = int(random()*256) for label in self.labels: if random() < 0.5: x, y = label.winfo_x(), label.winfo_y() label['foreground'] = "#%02x%02x%02x" % ( (red + y*30) % 256, (green + x/3) % 256, blue) else: label['foreground'] = label['background'] self.frame.after(self.speed, self.blinker) if __name__ == '__main__': # For stand-alone usage root = Tk() blink = Blink(root) root.mainloop()