Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi Projects for Kids

You're reading from  Raspberry Pi Projects for Kids

Product type Book
Published in Mar 2014
Publisher
ISBN-13 9781783982226
Pages 96 pages
Edition 1st Edition
Languages
Author (1):
Daniel Leonard Bates Daniel Leonard Bates
Profile icon Daniel Leonard Bates

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), 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)

The preceding line of code says that whenever Button-1 (the left mouse button) is clicked, run the canvasclick function. We'll write that 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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}