directory

    • Tkinter library drawing basic ideas
    • Draw a dotted line
    • Defines the function that draws the original character
    • Click on the blink of an eye
    • Movement of limbs
    • The color change

We know that Python is powerful in large part because it has many powerful third-party libraries.

And one aspect of Python that stands out is the implementation of the code’s user interface, also known in Python as GUI programming.

Meanwhile, the most commonly used library for Python GUI programming is Tkinter, which contains many functions needed for Python interactive interface programming.

Recently, there is A customer demand to achieve cartoon character click action in GUI interface, so today I will share with you A tkinter library using canvas to draw A moving Doraemon operations.

Tkinter library drawing basic ideas

When using Python’s Tkinter library for interface drawing, you must set the Canvas Canvas. If you are familiar with tkinter library, you will know this function.

After setting up the Canvas, it is time for our programmer painter to display his talent. Here is a little advice for the big bad Wolf to his friend, that is, before Canvas painting.

You can first set a coordinate center of x and Y axis according to the location of the canvas. The purpose of this is to facilitate the layout setting of the graphical interface according to coordinate parameters when drawing with codes. The specific codes for setting coordinate axes are as follows:

Draw a dotted line

   line1 = w.create_line(0.200.400.200,fill="black",dash=(4.4))    # set the horizontal coordinate line

    line2 = w.create_line(200.0.200.400,fill="black",dash=(4.4))    # Set the vertical line
Copy the code

After setting up the axes, doraemon’s masterpiece begins.

In fact, the use of programming language graphics drawing, most are the use of regular or irregular graphics assembly splicing

It’s like playing with building blocks when you’re a kid. All you have to do is take different building blocks (shapes drawn by code) and assemble them into the shape you want.

So the graph drawing problem, the most important is what shape of the graph where to place the problem.

So the big bad Wolf will share with you how to draw doraemon’s cartoon picture:

Defines the function that draws the original character

def bt1_age():


```python
 # painting dotted line

    line1 = w.create_line(0.200.400.200,fill="black",dash=(4.4))    # set the horizontal coordinate line

    line2 = w.create_line(200.0.200.400,fill="black",dash=(4.4))    # Set the vertical line

    # draw a round face

    w.create_oval((125.70.275.220),fill = 'blue')

    # draw a round face

    w.create_oval((140.100.260.220),fill = 'white')

    # draw ellipse

    w.create_oval((200.80.230.120),fill = 'white')

    # draw ellipse

    w.create_oval((170.80.200.120),fill = 'white')

    # Paint oval eyes

    w.create_oval((203.92.215.108),fill = 'black')

    # Paint oval eyes

    w.create_oval((185.92.197.108),fill = 'black')

    # Paint oval eyes

    w.create_oval((206.95.212.105),fill = 'white')

    # Paint oval eyes

    w.create_oval((188.95.194.105),fill = 'white')

    # Draw a round nose

    w.create_oval((193.115.207.130),fill = 'red')

    # mouth

    w.create_arc((140.60.260.180),style = 'arc',start = 240,extent = 60)# draw arc

    w.create_line(200.130.200.180,fill="black")# a vertical bar

    # beard

    w.create_line(215.150.245.150,fill="black")# horizontal

    w.create_line(155.150.185.150,fill="black")# horizontal

    w.create_line(158.127.185.137,fill="black")

    w.create_line(215.137.242.127,fill="black")

    w.create_line(158.170.185.163,fill="black")

    w.create_line(215.163.242.168,fill="black")

    # body

    w.create_rectangle(150.200.250.285,fill="blue")     # Body front

    w.create_arc((160.190.240.270),style = 'chord',start = 135,extent = 270,fill = 'white')# draw arc

    w.create_line((150.200.250.200),fill="red",width = 10,capstyle = 'round')# Rounded outline

    w.create_arc((185.270.215.300),style = 'chord',start = 0,extent = 180,fill = 'white')# draw arc

    w.create_line(185.285.215.285,fill="white")

    # painting feet

    # draw ellipse

    w.create_oval((140.275.190.295),fill = 'white')     # left foot

    w.create_oval((210.275.260.295),fill = 'white')     # right foot

    # hand painting

    w.create_polygon([150.205.150.235.120.250.120.235],outline="black",fill="blue")# Arm polygon

    w.create_polygon([250.205.250.235.280.250.280.235],outline="black",fill="blue")

    w.create_oval((110.230.135.255),fill = 'white')     # Left rounded palm

    w.create_oval((265.230.290.255),fill = 'white')     # Right round palm

    # draw the bell

    w.create_oval((190.200.210.220),fill = 'yellow')    # Bell center circle

    w.create_line((191.210.209.210),fill="black",width = 5,capstyle = 'round')# Rounded outline

    w.create_line((192.210.208.210),fill="yellow",width = 3,capstyle = 'round')# Rounded outline

    w.create_oval((198.213.202.217),fill = 'red')   # Red dot in the center

    w.create_line(200.218.200.220,fill="black")

    # drawing pocket

    w.create_arc((170.200.230.260),style = 'chord',start = 180,extent = 180,fill = 'white')# draw arc
Copy the code

These are the basic operations that define Doraemon’s functions, so the next step is to make Doraemon do all the work.

In fact, the realization of each action of cartoon characters is actually a brand new picture. Through a number of such picture functions, when clicking the button triggering an action, the picture of the function can be called.

So the big bad Wolf will share with you a few interesting changes of action.

Click on the blink of an eye

The blink function is mostly the same as the one used to define Doraemon. Since it is A blink, the biggest difference is in the eyes.



So once you’ve defined the blink function, all you need to do is replace the eye code with the eye closure code below,

Eye closing action code

# close your left eye
    w.create_line((185.100.195.105), fill='black')   # close your left eye
    w.create_line((185.105.195.105), fill='black')   # close your left eye
    w.create_line((185.110.195.105), fill='black')   # close your left eye

    # close your right eye
    w.create_line((205.105.215.100), fill='black')   # close your right eye
    w.create_line((205.105.215.105), fill='black')   # close your right eye
    w.create_line((205.105.215.110), fill='black')   # close your right eye
Copy the code

Movement of limbs



The function of moving the limbs is relatively complex, because the arms and legs need to move at the same time, so it is necessary to modify the arm code and doraemon’s body part, the specific implementation code is as follows:

Modify parameters to achieve limb movement

  w.create_rectangle(150.200.250.285,fill="blue")     # Body front

    w.create_arc((160.190.240.270),style = 'chord',start = 135,extent = 270,fill = 'white')# draw arc

    w.create_line((150.200.250.200),fill="red",width = 10,capstyle = 'round')# Rounded outline

    w.create_arc((170.270.230.300),style = 'chord',start = 0,extent = 180,fill = 'white', outline = 'white')# Arc your legs wide open

    w.create_line(185.285.215.285,fill="white")

    # painting feet

    # draw ellipse

    w.create_oval((130.275.170.295),fill = 'white')

    w.create_oval((230.275.270.295),fill = 'white')
Copy the code

The following section is to modify the implementation of arm movement code modification: modify parameters to achieve limb movement

 w.create_rectangle(150.200.250.285,fill="blue")     # Body front

    w.create_arc((160.190.240.270),style = 'chord',start = 135,extent = 270,fill = 'white')# draw arc

    w.create_line((150.200.250.200),fill="red",width = 10,capstyle = 'round')# Rounded outline

    w.create_arc((170.270.230.300),style = 'chord',start = 0,extent = 180,fill = 'white', outline = 'white')# Arc your legs wide open

    w.create_line(185.285.215.285,fill="white")

    # painting feet

    # draw ellipse

    w.create_oval((130.275.170.295),fill = 'white')

    w.create_oval((230.275.270.295),fill = 'white')
Copy the code

The color change

Read duo la A dream’s small partner should all know duo la A dream has A younger sister —- A yellow duo la A dream.



So we only need to modify part of the code to achieve the color change of Doraemon, in this case, we only need to change all the code that calls’ blue ‘to call’ yellow ‘when drawing, and we can also draw other colors according to our own preferences.

So our Doraemon and the realization of A variety of different action changes of the function has been defined, summed up is in the canvas to define different graphics function, according to the need to call the function can be!

Complete code can be downloaded at https://download.csdn.net/download/weixin_44985880/12367814. * * or concern WeChat public, “the Wolf hole primary” reply “doraemon” duo la A dream full of the GUI programming code! 支那