Python Turtle Graphics Tutorial #4 - Drawing With Mouse

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·7y ago

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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This tutorial teaches how to create a drawing program using Python's Turtle graphics module that follows the mouse, allowing users to draw objects on the screen. It covers creating functions for dragging and clicking the mouse, and introduces the screen and turtle objects.

Key Takeaways
  1. Import the Turtle graphics module
  2. Create a screen object
  3. Create a turtle object
  4. Define a dragging function
  5. Define a click function
  6. Set up the mouse events
  7. Run the main loop
💡 The Turtle graphics module can be used to create interactive graphics programs that respond to user input, such as mouse events.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →