Tkinter find the black dot illusion

vegaseat 2 Tallied Votes 695 Views Share

Python programming can have its lighter moments. Here we use one of those moments to draw an optical illusion using the Tkinter canvas to draw a rectangle, some lines and circles.

# use the Tkinter canvas to draw a
# "find the black dots" optical illusion
# vegaseat  09aug2010

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()
root.title("find the black dots")

# create the drawing canvas
cv = tk.Canvas(root, width=515, height=460, bg='white')
cv.pack()

# draw a black rectangle
# upper left corner coordinates (x1, y1)
x1 = 10
y1 = 10
# lower right corner coordinates (x2, y2)
x2 = 505
y2 = 450
cv.create_rectangle(x1, y1, x2, y2, fill="black")

# draw grey horizontal lines
x1 = 10
x2 = 506
for k in range(60, 450, 50):
    y1 = k
    y2 = k
    cv.create_line(x1, y1, x2, y2, width=10, fill="grey")

# draw grey vertical lines
y1 = 10
y2 = 451
for k in range(60, 490, 50):
    x1 = k
    x2 = k
    cv.create_line(x1, y1, x2, y2, width=10, fill="grey")

# draw small white circles where the lines cross
for x in range(460, 20, -50):
    for y in range(60, 450, 50):
        circle = cv.create_oval(x+10, y+10, x-10, y-10, fill='white')

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.