Reader small image

You're reading from  Raspberry Pi Projects for Kids (Second Edition)

Product typeBook
Published inApr 2015
Reading LevelBeginner
Publisher
ISBN-139781785281525
Edition1st Edition
Languages
Right arrow
Author (1)
Daniel Leonard Bates
Daniel Leonard Bates
author image
Daniel Leonard Bates

Daniel Bates is a computer science researcher at the University of Cambridge. His day job involves inventing designs for future mobile phone processors and when he gets home, he likes playing games or working on one of his coding projects (or both!). Daniel has been a volunteer for the Raspberry Pi Foundation since 2011 and is enthusiastic about introducing new people to computing. He has previously written Instant Minecraft: Pi Edition Coding How-to and Raspberry Pi Projects for Kids (First Edition), both published by Packt Publishing.
Read more about Daniel Leonard Bates

Right arrow

Adding markers


The next thing we want to do is add a marker to the map whenever we click on it with the mouse. This can be done in two parts: by detecting the click and reacting to the click.

Detecting mouse clicks

Detecting mouse clicks is very simple. Tkinter does most of the work for us. All we have to do is bind a function to the mouse button. Once the program has entered its main loop, whenever the mouse button creates an event (by being clicked on), the function will be executed. Reacting to an event in this way is similar to using a when key pressed code block in Scratch. Place the following line of code with the rest of the Canvas code before the main loop:

canvas.bind("<Button-1>", canvasclick)

This code says that whenever Button-1 (the left mouse button) is clicked on, run the canvasclick function. We'll write this function next.

We can create these bindings for as many buttons and keys as we like and for any widget that we like. The "<Button-3>" button is the right mouse...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Raspberry Pi Projects for Kids (Second Edition)
Published in: Apr 2015Publisher: ISBN-13: 9781785281525

Author (1)

author image
Daniel Leonard Bates

Daniel Bates is a computer science researcher at the University of Cambridge. His day job involves inventing designs for future mobile phone processors and when he gets home, he likes playing games or working on one of his coding projects (or both!). Daniel has been a volunteer for the Raspberry Pi Foundation since 2011 and is enthusiastic about introducing new people to computing. He has previously written Instant Minecraft: Pi Edition Coding How-to and Raspberry Pi Projects for Kids (First Edition), both published by Packt Publishing.
Read more about Daniel Leonard Bates