Created
June 15, 2016 06:22
-
-
Save JaimieMurdock/f895324c47b9a98b070c125dedff4749 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
import random | |
root = Tk() | |
colors = ['navy', 'slate blue', 'lime green', | |
'purple', 'firebrick', 'salmon', | |
'slate gray'] | |
# Exit button | |
def exit(): | |
root.destroy() | |
b1 = Button(root, text="Exit", command=exit) | |
b1.pack(side=BOTTOM,padx=20) | |
def clear(): | |
canvas.delete('all') | |
clear_button = Button(root, text="Clear", command=clear) | |
clear_button.pack(side=BOTTOM, padx=20) | |
def undo(): | |
global MEMORY | |
global CLICK_COUNTS | |
number_to_delete = CLICK_COUNTS[-1] | |
for index in range(-number_to_delete, 0): | |
canvas.delete(MEMORY[index]) | |
del MEMORY[index] | |
del CLICK_COUNTS[-1] | |
undo_button = Button(root, text="Undo", command=undo) | |
undo_button.pack(side=BOTTOM) | |
canvas = Canvas(root, width=400, height=400) | |
canvas.pack() | |
def get_pointer(event=None): | |
x = root.winfo_pointerx() - root.winfo_rootx() | |
y = root.winfo_pointery() - root.winfo_rooty() | |
print("You clicked at {}, {}".format(x, y)) | |
return x, y | |
MEMORY = [] | |
def draw_rectangle(x, y): | |
shape = canvas.create_rectangle( | |
x, y, x + 10, y + 10, | |
fill=random.choice(colors)) | |
global MEMORY | |
MEMORY.append(shape) | |
LOOP = True | |
CLICK_COUNTS = [] | |
def draw(event=None): | |
x, y = get_pointer() | |
draw_rectangle(x, y) | |
global CLICK_COUNTS | |
CLICK_COUNTS[-1] = CLICK_COUNTS[-1] + 1 | |
global LOOP | |
if LOOP: | |
root.after(1, draw) | |
def start_draw(event=None): | |
global CLICK_COUNTS | |
CLICK_COUNTS.append(0) | |
global LOOP | |
LOOP = True | |
draw() | |
def stop_draw(event=None): | |
global LOOP | |
LOOP = False | |
global MEMORY | |
print("There are {} shapes".format(len(MEMORY))) | |
canvas.bind('<Button-1>', start_draw) | |
canvas.bind('<ButtonRelease-1>', stop_draw) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment