-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram_17_Tk_Draw.py
More file actions
126 lines (102 loc) · 4.88 KB
/
program_17_Tk_Draw.py
File metadata and controls
126 lines (102 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#----------------------------------------------------------
# These websites are a good reference for Tk
# http://www.tkdocs.com/tutorial/index.html
# http://www.pythonware.com/library/tkinter/introduction/
#----------------------------------------------------------
#---------------------------------------------------------------------
# This program shows how to use a Tk Canvas to draw using the mouse
#---------------------------------------------------------------------
from tkinter import *
from tkinter import ttk
# These global variables hold the coordinates ( x, y ) of the mouse
xCoord = 0
yCoord = 0
# These global variables are the pen color and width
color = "black"
lineWidth = 1.
#---------------------------------------------------------------------
# function setXY is called when the mouse button 1 is pressed.
# It stores the x, y values of the mouse into the global variables.
# Note that we have to declare the xCoord, yCoord as global
# otherwise we can't write new values to them from inside
# a function
#---------------------------------------------------------------------
def setXY(event):
global xCoord, yCoord
xCoord = event.x
yCoord = event.y
#---------------------------------------------------------------------
# function setColor() changes the color of the pen
# the newColor is passed in, and then used to set the global color
#---------------------------------------------------------------------
def setColor(newColor):
global color
color = newColor
#---------------------------------------------------------------------
# function setLineWidth() changes the color of the pen
# the newColor is passed in, and then used to set the global color
#---------------------------------------------------------------------
def setLineWidth(newWidth):
global lineWidth
lineWidth = newWidth
#---------------------------------------------------------------------
# function addLine() draws a line as the mouse is moved with
# the first button held down
#---------------------------------------------------------------------
def addLine(event):
global xCoord, yCoord
# use the Canvas().create_line() method
# Note that the line is drawn from the xCoord, yCoord which
# is the starting point (the last values of x, y) and is
# drawn to event.x, event.y which come from the mouse
canvas.create_line((xCoord, yCoord, event.x, event.y),
fill = color, width = lineWidth)
# update the x, y coordinates to the ending position
setXY(event)
#---------------------------------------------------------------------
# delete all items from the canvas and redraw the legend
#---------------------------------------------------------------------
def Clear(event):
# Note that the canvas is created in the main section below
canvas.delete(ALL)
CreateLegend()
#---------------------------------------------------------------------
# Create 3 color rectangles that the user can click on to
# change the color of the pen, and 3 lines the user can
# click to set the line width
#---------------------------------------------------------------------
def CreateLegend():
# this creates a red rectangle
id = canvas.create_rectangle((10, 10, 30, 30), fill="red")
# this binds the setColor("red") function to the mouse click
canvas.tag_bind(id, "<Button-1>", lambda x: setColor("red"))
id = canvas.create_rectangle((10, 35, 30, 55), fill="blue")
canvas.tag_bind(id, "<Button-1>", lambda x: setColor("blue"))
id = canvas.create_rectangle((10, 60, 30, 80), fill="black")
canvas.tag_bind(id, "<Button-1>", lambda x: setColor("black"))
# this creates a thin vertical line
id = canvas.create_line((10, 90, 10, 120),
fill = "black", width = 1.0)
# this binds the setLineWidth(1.0) function to the mouse click
canvas.tag_bind(id, "<Button-1>", lambda x: setLineWidth(1.0))
id = canvas.create_line((20, 90, 20, 120),
fill = "black", width = 3.0)
canvas.tag_bind(id, "<Button-1>", lambda x: setLineWidth(3.0))
id = canvas.create_line((30, 90, 30, 120),
fill = "black", width = 6.0)
canvas.tag_bind(id, "<Button-1>", lambda x: setLineWidth(6.0))
#---------------------------------------------------------------------
root = Tk()
root.title('Tk Canvas Draw')
# Tell the root window to resize to fill all available space
root.columnconfigure(0, weight = 1)
root.rowconfigure (0, weight = 1)
# Create the Tk Canvas to draw on
canvas = Canvas(root)
canvas.grid( column = 0, row = 0, sticky=(N, W, E, S) )
# Connect the mouse button to the functions setXY, addLine and Clear
canvas.bind("<Button-1>", setXY) # Button-1 is the left button
canvas.bind("<B1-Motion>", addLine) # Button-1 press plus moving mouse
canvas.bind("<Button-3>", Clear) # Button-3 is the right button
CreateLegend()
root.mainloop()