GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — How to Make a Drawing Program in Python - The Python Code
Want the complete games? Get the source code of 5 polished Python games
(Tetromino Pro, Flappy Wings Pro, Chess Pro, Snake Pro, Platformer Pro) in the Game Dev Pro Pack.
$29
In this tutorial, we will make a simple drawing program in Python using PyGame. We will utilize the buttons we have made in an earlier article to make it possible to switch colors and change the brush size; we'll also implement a saving feature. As always, we start with the imports. Because we use PyGame, we also have to keep in mind to install it first with the following command:
We also import
Related: How to Make a File Explorer using Tkinter in Python. Pygame ConfigurationWe continue with the After that, we make the object for that, and then we declare the starting Last but not least, we import a
VariablesNow we make some variables to use later. We start with a list called After that, we set the initial In the end, we define the dimensions of our canvas, and this is the area where we will be able to draw:
Button ClassWe use the button class from this tutorial (feel free to go and copy/paste or get the complete code here). So we insert its class here; we have already made the
Handler FunctionsNow we will set up three functions to handle the color and brush size changes and the save request. Change ColorThe
Change Brush SizeThe
Save to DiskThanks to the
Button SetupWe continue by setting up our buttons. We first define two variables that represent the button width and height:
After that, we make a list that consists of more lists where each first element is the displayed text and each second element is the function to be called:
We will now loop over this list and generate buttons. They will all be in a row and have 10-pixel gaps. This won't look pretty, but it will do the work for our little application:
CanvasNow we use the
Related: How to Make a Planet Simulator with PyGame in Python. Main LoopNow for the exciting part. As usual, we We are also looping over our
DrawingBefore we make the draw function, we
Next, we check if the left mouse button was pressed with
In the end, we also draw a circle which shows the users how large the brush is and what color it is:
Showcase
Amazing, see how you can customize the tool however you want! Get the full code here. Here are some related tutorials:
If you want to learn more about using Tkinter, check this tutorial where you create a calculator app along with many features! Alternatively, If you want to build more GUIs with Python, check our GUI programming tutorials page! Happy coding ♥
Premium source code
Build real games: Game Dev Pro PackAll five premium Python games in one download: Tetromino, Flappy Wings, Chess, Snake and Platformer Pro.
Get all 5 games · $29
Sharing is caring!
Read also
How to Make a Text Editor using Tkinter in PythonLearn how to build a simple text editor that opens and saves text files using Tkinter library in Python
How to Make Buttons in PyGameLearn how to make buttons in PyGame that support pressed calling (multi pressing) and one-shot pressing in Python.
How to Make a Typing Speed Tester with Tkinter in PythonLearn how to build a UI-based typing speed tester in Python using the built-in Tkinter library. CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |