Python Turtle Graphics Tutorial #4 - Drawing With Mouse
Key Takeaways
This video tutorial covers using Python's Turtle graphics module to create a drawing program that follows the mouse, allowing users to draw objects on the screen using their mouse. The tutorial introduces the screen and turtle objects, and explains how to create functions for dragging and clicking the mouse.
Full Transcript
hey guys and welcome to the fourth video in my Python turtle graphics series I guess if you want to call it that and in today's video I'm going to be showing you some cool things that we can do with the mouse and how we can draw shapes kind of using the mouse and using like user input and it's actually pretty cool so I'll just show you actually what it looks like first of all because I already have it coated and I'll just gonna walk you through how we can create this pretty much you can kind of just draw whatever you want to draw and then if you want to erase at all you can right click and it will remove it now this combined with some of the stuff I've shown you in previous videos is pretty cool and you guys can kind of play around at that but anyways what we're gonna be doing for this is I'm gonna be introducing you to another part of the turtle what module which is the screen now I don't think I talked about this in previous videos but what we're gonna have to start off by doing is importing turtle and we're gonna say from turtle you're going to import turtle and we're going to import screen like this okay now we're simply just going to create a new screen object to make this equal to screen and then we're gonna create a new turtle object and in this case I'm just gonna say T is equal to + turtle and in this case I can just put turtle like this now this is a different way to create a turtle object it's just because we've imported it this way rather than just importing it like this okay so now I'm just gonna set the speed of our turtle to negative 1 now what this does is just gonna give us the maximum speed for our turtle okay so I'm gonna have to create two functions here so one of these functions is gonna be for when we click the right mouse button and the other function is going to be for dragging our turtle around the screen so the first one I'm going to do is called dragging okay it's gonna take two parameters in X&Y alright and then we're just gonna say T which their trail object dot on drag okay and you can just put none in here and you don't need anything and then what we're going to be doing to actually change direction of our turtle is we're going to set the heading of our turtle to be towards towards the X&Y and what this X&Y is gonna be is it's actually going to be the what do you call it like the mouse object right so what this sorry I just butchered that explanation but it's gonna be where your mouse is so whenever we have the turtle we start dragging it we want it to be pointing towards our mouse and that's what this does is it changes the heading to point towards our mouse and then we need to actually move the turtle object so we're changing the heading but now we have to move it to wherever our mouse position is so we can say t go to and then X comma Y okay and then what we're gonna do is we're gonna say T dot on drag and they were say dragging okay so what this means now is if you continue to drag the turtle object it's gonna continue to call this dragging function and set the heading and keep moving the object okay so the next function we need is called click right alright and this is pretty self-explanatory when you click the right mouse button this is what is gonna happen so in this case we are just gonna simply clear the screen and it's really easy to do that all you do is just T dot clear okay so now we actually need one more function but it's not really think of it is a function but it's just gonna run our main loop so I'm just gonna call this main okay and what I'm gonna do in here and say turtle dot listen just as I did in the last video and what this is gonna do is it's gonna listen for any like Mouse events and key events so that we can kind of see what to do when that happens so first of all we're gonna say T dot on drag okay so when we are dragging the turtle object that's what this method does then let us simply call dragging okay and that's gonna pass us in an x and y value and we'll be able to then do everything that I've talked about in dragging once that happens okay the next thing we do is rinse a turtle dot on screen click I believe the best yes on screen click and then in here we go to do click write and three so just so that talked to in the previous video this means our right mouse button if you wanted to be the leftmost button you put one middle mouse button would be to but in this case we need three and on screen click just means we click anywhere on the screen not just on the turtle object and then then we're simply gonna do screen dot main look like this and what this is gonna do is to continue looping through this main function until we click the X button on our turtle screen or until like we exit out so now when you just need to call the main function and this is actually it this our entire script so let's see how it works okay so here we go you can see if I grab this I can move it around the screen and if I right click ah or right clicking is not working right now that is strange I gotta make sure gotta see what is happening there click right I don't think I need the brackets for that let's see if that changes anything Oh No now we're getting an issue hmm interesting ok let's see what the issue is ah click rates I need to put X&Y in click right here so just make sure you add these two parameters and let's try this again there we go so now it's clear ok awesome so now that you guys know how to do this you can combine this with maybe changing like the thickness of the pen or the speed of the turtle or you can make it so if you right click or middle click then you draw a shape or you do whatever stuff you want to do like that ok so I just wanted to show you this I know it hasn't been like a very long video and I didn't do very much but I just want to show you this so you can kind of combine it with other stuff and you understand how to get the turtle object to follow along with the mouse object because that's typically something you will want to do so anyways that has pretty much been it for my turtle series and this tutorial now if you guys are a little bit disappointed that this is all I'm showing in the toriel's there's not that much to do in turtle and I would encourage you to move on to pygame if you're worried about like graphics and visuals because that's a much better module to be doing that with I have a lot of tutorial series on my channel where you can check out pygame I'll leave a link to a series in the description if you'd want to check that out and in case you want to learn more about turtle what you guys can always do is simply just go on the what do it go on the internet like this so I'll go on and just literally type turtle Python and read through the documentation yourself and it'll tell you all the different things you can do so it shows you all the different methods creating a circle to greet like all this stuff and you guys can read through this and kind of figure out what stuff you want to really do with turtle ok so anyways that's been it for this video if you guys enjoyed the series please make sure you leave a like and subscribe to the channel and I hopefully will see you again in another video [Music]
Original Description
In this python turtle graphics tutorial I explain how to have the turtle follow your mouse. This will allow us to draw objects using our mouse on the turtle screen.
Download Code: https://techwithtim.net/tutorials/python-module-walk-throughs/turtle-module/drawing-with-mouse/
Pygame Tutorials: https://www.youtube.com/watch?v=i6xMBig-pP4&t=2s
Turtle Module Documentation: https://docs.python.org/2/library/turtle.html
**************************************************************
Support the Channel: https://www.patreon.com/techwithtim
Join my discord server: https://discord.gg/pr2k55t
**************************************************************
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Python Tutorials
- Turtle Module
- Python Turtle
- Python Turtle Tutorial
- Turtle Python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
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
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
🎓
Tutor Explanation
DeepCamp AI