Tkinter find the black dot illusion UserPageVisits:348 active 80 80 DaniWeb 561 60 2010-08-09T23:48:56+00:00 https://www.daniweb.com/programming/software-development/code/303171/tkinter-find-the-black-dot-illusion

Tkinter find the black dot illusion

vegaseat

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.

348 Views
About the Author

Scientist

code snippet
# 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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.