Random Graphics (Python)

vegaseat 0 Tallied Votes 1K Views Share

Years ago I wrote a little screen-saver in Delphi that randomly put colorful circles all over the screen. A hit with the secretaries in the office. I modified a simple Tkinter based snippet to put the same colorful circles all over a window form at random locations, random radii and random colors.

# random circles in Tkinter
# a left mouse click will idle action for 5 seconds
# tested with Python24    vegaseat    14sep2005

from Tkinter import *
import random
import time

def idle5(dummy):
    """freeze the action for 5 seconds"""
    root.title("Idle for 5 seconds")
    time.sleep(5)
    root.title("Happy Circles ...")

# create the window form
root = Tk()
# window title text
root.title("Happy Circles ...")
# set width and height
w = 640
h = 480
# create the canvas for drawing
cv = Canvas(width=w, height=h, bg='black')
cv.pack()
# list of colors to pick from
colorList = ["blue", "red", "green", "white", "yellow", "magenta", "orange"]
# endless loop to draw the random circles
while 1:
    # random center (x,y) and radius r
    x = random.randint(0, w)
    y = random.randint(0, h)
    r = random.randint(5, 50)
    # pick the color
    color = random.choice(colorList)
    # now draw the circle
    cv.create_oval(x, y, x+r, y+r, fill=color)
    # update the window
    root.update()
    # bind left mouse click, idle for 5 seconds
    cv.bind('<Button-1>', idle5)

# start the program's event loop
root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.