Drawing board

Make a drawing board using Tkinter!

When I programmed Lesson 7-3 in the textbook I dealt with in the class, I thought that I could draw a line if I did this drawing continuously, but I made it into a shape while investigating.


Difference from Lesson 7-3

――In the textbook, you programmed to draw a circle by clicking, but this time it is drawn all the time while the mouse button is pressed. ――To draw a line, it becomes unnatural if the drawn circle is large, so I made it possible to draw a more natural line by drawing as small a circle as possible.


Code introduction

I will introduce the code that was actually programmed.


import tkinter

def mouse_click_func(event):
    global press

    press = True

First, here we define clicking the mouse button.


def mouse_release_func(event):
    global press

    press = False

Next, define that you release the mouse button.


def mouse_move_func(event):
    global canvas

    x = event.x
    y = event.y

    if press:
        canvas.create_oval(x - 2, y - 2, x + 2, y + 2, fill = "white", width=0)

And finally, we define drawing a circle when the mouse is moving and the button is held down.


In making the code

In order to draw a circle continuously, it is necessary not only to turn the button on and off, but also to separate the cases of ** 3 patterns that are ON and the mouse is moving **. At first, I was thinking of moving the mouse while pressing the button and releasing the button, but I didn't know what to do. So, when I referred to the site, I was impressed when I saw that the press was set to true false by turning the button on and off.


Execution result

I tried to make it look like a blackboard! 2020-07-10 (3).png


Improvement point 1

If you move the mouse quickly, the lines will not be clean. 2020-07-10 (2).png It seems that this is a processing problem of the personal computer. It may be possible to improve if the method is not a method of drawing dots continuously.


Improvement point 2

Once you make a mistake, you cannot erase it. Since it is decided whether to draw a dot by pressing or not pressing the mouse button, there is not enough definition to perform the action of erasing the dot. It may be possible to erase the case if something can be done before pressing the mouse button.


Impressions

It was my first time to come into contact with programming, so there were some difficult parts, but I enjoyed working on it. I'm interested, so I'll try to work on it voluntarily.


--Reference

Create a GUI app using Tkinter in python [https://daeudaeu.com/programming/python/tkinter/python_tkinter/]

The easiest Python introductory class by Fumitaka Osawa

Recommended Posts

Drawing board
[python] Streamline drawing
RK3399 board buildroot