Controlling mouseclicks
Deprecated Lab — Trinket is shutting down
This lab was written for Trinket.io, which is shutting down in August 2026. The embedded trinket.io links on this page will stop working after that date.
These pages are kept for reference only. The current version of this course now runs every lab as an inline Skulpt editor right in the page — no account or install needed. Start at Chapter 1: Welcome to Python.
Getting a handle of the mouse clicks
We can use trinkets to change the background and control mouse clicks.
Also we have access to the exact position where the mouse was clicked. That creates a lot of possibilities
for creating different interactions with the screen.
Code to change screen color
Code to get co-ordinates of mouse click
| x=turtle.xcor()
y=turtle.ycor()
|
Here is an example of setting the screen color. We then draw a square in the center of the screen.
When the user clicks a circle is drawn within the circle. On next click the circle increases in diameter. When the circle touches the square, it stops increaing in size.
If the user clicks the screen outside the square the screen is refreshed
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 | #import packages
import turtle
import random
# global colors
col = [ 'yellow', 'green', 'blue',
'white', 'orange', 'pink']
circlecol = [ 'red', 'magenta', 'purple',
'black', 'brown', 'turqouise' ]
# method to call on screen click
tina = turtle.Turtle()
circlediameter=10
squaresize = 200
def drawCircleOnMouseClick(x, y):
global circlecol
global circlediameter
print(" x={}, y={}".format(x,y))
##If mouse is clicked within square increase size, if mouse clicked outside reset screen
if (isMouseClickWithinSquare(x,y)) :
## Check if diameter is same size as square
if (circlediameter <= squaresize/2):
print(" circlediameter={}, squaresize={}".format(circlediameter,squaresize))
##If yes do not increase the size else increase the size
circlediameter += 10
##If mouse is clicked outside the square, reset the screen - draw a new square
else:
screenreset()
draw_circle(tina, circlediameter, getCircleColor() )
def getBgColor():
ind = random.randint(0, 5)
return col[ind]
def getCircleColor():
ind = random.randint(0, 5)
return circlecol[ind]
def drawsquare(turtle, size, color, startx, starty):
turtle.penup()
turtle.goto(startx, starty)
turtle.pendown()
turtle.fillcolor(color)
turtle.color(color)
turtle.begin_fill()
for i in range(4):
turtle.forward(size)
turtle.left(90)
turtle.end_fill()
def draw_circle(turtle, diameter, color):
turtle.penup()
turtle.goto(0, -diameter)
turtle.pendown()
turtle.fillcolor(color)
turtle.color(color)
turtle.begin_fill()
turtle.circle(diameter)
turtle.end_fill()
def screenreset():
tina.reset()
drawsquare(tina, squaresize, getBgColor(), -100, -100 )
def isMouseClickWithinSquare(x, y):
return (x >= -100 and x <= 100 and y >= -100 and y <= 100)
# set screen
sc = turtle.Screen()
sc.setup(400, 400)
sc.bgcolor('skyblue')
screenreset()
##Screen can respond to mouseclick and we can tell it what action to take i.e. method to call
# call method on screen click
sc.onscreenclick(drawCircleOnMouseClick)
|
Sample Program
Sample
Experiments
- Can you draw different shapes
- Can you add some more colors in the arrays
- Can you change background color of the screen
- Can you try some other events