# =========================================================
# Draw "stuff"
# =========================================================
# ---------------------------------------------------------
# --- imports
# ---------------------------------------------------------
import sys
# --- what version of Python an I running?
if sys.version_info.major == 3:
from tkinter import *
from tkinter.font import *
py3 = True
else:
from Tkinter import *
from tkFont import *
py3 = False
# ---------------------------------------------------------
# -- global variables
# ---------------------------------------------------------
canvasWidth = 500
canvasHeight = 500
buttonFont = ('arial', 12, 'bold')
titleFont = ('arial', 14, 'bold')
#drawingAreaColors = ['red', 'light blue', 'green', 'orange']
#penColors = ['black', 'white', 'purple', 'yellow']
defaultBgColor = 'white'
defaultPenColor = 'black'
penColor = defaultPenColor
# ---------------------------------------------------------
# --- functions
# ---------------------------------------------------------
def quit():
sys.exit()
def reset():
global penColor, defaultBgColor
can.config(bg=defaultBgColor)
can.delete(ALL)
penColor = defaultPenColor
def setDrawingAreaColor(color):
can.configure(bg=color)
def setPenColor(color):
global penColor
penColor = color
def paint(event):
global penColor
x1,y1 = (event.x -4), (event.y -4)
x2,y2 = (event.x +4), (event.y +4)
can.create_oval(x1, y1, x2, y2,
outline=penColor, fill=penColor)
# ---------------------------------------------------------
# --- main
# ---------------------------------------------------------
# --- root window
root = Tk()
root.title('Drawing Program')
# --- frame to hold everything
f = Frame(root, relief='flat', padx=4, pady=4)
f.grid(row=0, column=0, sticky=N+S+E+W)
# --- title label
lt = Label(f, text='Drawing Program', font=titleFont)
lt.grid(row=0, column=0)
# --- canvas drawing area
can = Canvas(f, borderwidth=4, relief='ridge',
width=canvasWidth, height=canvasHeight, bg=defaultBgColor)
can.grid(row=1, column=0)
# --- bind mouse button event
can.bind('<B1-Motion>', paint)
# --- frame for buttons
fb = Frame(f, borderwidth=4, relief='ridge', padx=4, pady=4)
fb.grid(row=2, column=0)
# --- background color buttons
rw = 0 # row
cl = 0 # column
#for c in drawingAreaColors:
#b = Button(fb, text=c, bg=c, font=buttonFont,
# command=lambda:
# setDrawingAreaColor(strCopy(c)))
#b.grid(row=rw, column=cl, sticky=EW)
# cl += 1 # column
c = 'red'
b = Button(fb, text=c, bg=c, font=buttonFont,
command=lambda: setDrawingAreaColor('red'))
b.grid(row=rw, column=cl, sticky=EW)
cl += 1 # column
c = 'blue'
b = Button(fb, text=c, bg=c, font=buttonFont,
command=lambda: setDrawingAreaColor('blue'))
b.grid(row=rw, column=cl, sticky=EW)
cl += 1 # column
c = 'green'
b = Button(fb, text=c, bg=c, font=buttonFont,
command=lambda: setDrawingAreaColor('green'))
b.grid(row=rw, column=cl, sticky=EW)
cl += 1 # column
c = 'orange'
b = Button(fb, text=c, bg=c, font=buttonFont,
command=lambda: setDrawingAreaColor('orange'))
b.grid(row=rw, column=cl, sticky=EW)
# --- pen color buttons
rw += 1 # row
cl = 0 # column
#for c in penColors:
#b = Button(fb, text=c, bg=c, font=buttonFont,
# command=lambda: setPenColor(c))
#b.grid(row=rw, column=cl, sticky=EW)
#cl += 1 # column
c = 'black'
b = Button(fb, text=c, bg=c, font=buttonFont,
command=lambda: setPenColor('black'))
b.grid(row=rw, column=cl, sticky=EW)
cl += 1 # column
c = 'white'
b = Button(fb, text=c, bg=c, font=buttonFont,
command=lambda: setPenColor('white'))
b.grid(row=rw, column=cl, sticky=EW)
cl += 1 # column
c = 'purple'
b = Button(fb, text=c, bg=c, font=buttonFont,
command=lambda: setPenColor('purple'))
b.grid(row=rw, column=cl, sticky=EW)
cl += 1 # column
c = 'yellow'
b = Button(fb, text=c, bg=c, font=buttonFont,
command=lambda: setPenColor('yellow'))
b.grid(row=rw, column=cl, sticky=EW)
# --- reset button
rw += 1 # row
cl = 1 # column
b = Button(fb, text='Reset', font=buttonFont, command=reset)
b.grid(row=rw, column=cl, sticky=EW)
# --- quit button
cl = 2 # column
b = Button(fb, text='Quit', font=buttonFont, command=quit)
b.grid(row=rw, column=cl, sticky=EW)
# --- main loop
mainloop()