Storing and retrieving a mouse-drawn shape

We make a program that lets you create a shape through the use of the mouse and by means of three buttons we can store the shape on disk, clear the canvas and then recall and display the shape on the screen.

Getting ready

Ensure you have created a folder call constr because this is where the code in our program expects to be able to save the shape drawn. It is also where it will retrieve it from when commanded to retrieve and display it.

# mouse_shape_recorder_1.py
#>>>>>>>>>>>>>>>>>>>>>
from Tkinter import *
root = Tk()
root.title("Mouse Drawn Shape Saver")
cw = 600 # canvas width
ch = 400 # canvas height
chart_1 = Canvas(root, width=cw, height=ch, background="#ffffff")
chart_1.grid(row=1, column=1)
pt = [0]
x0 = [0]
y0 = [0]
count_point = 0
x_end = 10
y_end = 10
#============================================
# Create a new circle where the click happens and draw a new line
# segment to the last point (where the mouse was left clicked).
def callback_1(event): # Left button pressed.
global count_point, x_end, y_end
global x0, y0
global x0_n, y0_n, pt
x_start = x_end
y_start = y_end
x_end = event.x
y_end = event.y
chart_1.create_line(x_start, y_start , x_end,y_end , fill =  "#0088ff")
chart_1.create_oval(x_end-5,y_end-5, x_end+5, y_end+5, outline =  "#0088ff")
count_point += 1
pt = pt + [count_point]
x0 = x0 + [x_end] # extend list of all points
y0 = y0 + [y_end]
chart_1.bind("<Button-1>", callback_1) # <button-1> left mouse button
#==============================================
# 1. Button control to store segmented line
def callback_6():
global x0, y0
xy_points = open('/constr/shape_xy_1.txt', 'w')
xy_points.write(str(x0))
xy_points.write('
')
xy_points.write(str(y0))
xy_points.close()
Button(root, text="Store", command=callback_6).grid(row=0, column=2)
#=============================================
# 2. Button control to retrieve line from file.
def callback_7():
global x0, y0 # Stored list of mouse-click positions.
xy_points = open('/constr/shape_xy_1.txt', 'r')
x0 = eval(xy_points.readline())
y0 = eval(xy_points.readline())
xy_points.close()
print "x0 = ",x0
print "y0 = ",y0
for i in range(1, count_point): # Re-plot the stored and # retreived line
chart_1.create_line(x0[i], y0[i] , x0[i+1], y0[i+1] ,  fill = "#0088ff")
chart_1.create_oval(x_end - 5,y_end - 5, x_end + 5,  y_end + 5 , outline = "#0088ff")
Button(root, text="retrieve", command=callback_7).grid(row=1,  column=2)
#=============================================
# 3. Button control to clear canvas
def callback_8():
chart_1.delete(ALL)
Button(root, text="CLEAR", command=callback_8).grid(row=2, column=2)
root.mainloop()

How it works...

In addition to a callback function for adding the positions of left mouse clicks to lists x0 and y0, of x and y-coordinates, we have another three callback functions. The three additional callback functions are to trigger the execution of functions that:

  • Save the lists x0 and y0 to a disk in a file called shape_xy_1.txt.
  • Clear the canvas of all drawn lines and circles
  • Retrieve the contents of shape_xy_1.txt and re-draw it onto the canvas

There's more...

Drawing is an imperfect process and artists and draughtsman use an eraser as well as a pencil. When we make drawings with a mouse connected to a computer we also need to make adjustments and corrections to any lines we draw. We need editing ability.

Drawing is an imperfect process. We would like to be able to adjust the position of some of the points in order to improve the drawing. We do this in the next recipe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.255.168