Python Draw Bing Dwen Dwen Source Code

Introduction

The Beijing Winter Olympics are being held recently. The mascot of the Beijing Winter Olympics, "Bing Dwen Dwen", is very popular. Many friends can't get a Bing Dwen Dwen even if they line up overnight.

Bing Dwen Dwen is too popular. In order to realize the dream of Bing Dwen Dwen's freedom and "one Dwen per hand", the powerful programmer bro presents a program to draw Bing Dwen Dwen in Python. First, let's see the animation below to experience it.

It's so powerful, netizens are really omnipotent

Ideas and Code

Technically, the turtle and tkinter modules of python are mainly used. These two modules are built-in modules of Python and are very powerful.

  • turtle is used for drawing, there are many drawing methods and styles
  • tkinter is used to build GUI programs and also has many built-in components

The main program basically uses turtle, sets different drawing methods such as brushes and paths, and draws one stroke by one on the initialized tkinter Canvas. The core feature is completed.

First start a tkinter interface, place a Canvas, and then set the turtle drawing area to this Canvas

root = Tk()
root.geometry('600x700+500+60')
root.config(bg='white')
root.title('lwebapp.com')
root.resizable(False, False)
canvas = Canvas(root, width=600, height=600)
canvas.place(x=0,y=50)

t = RawTurtle(canvas)
t.hideturtle()

Check complete code to main.py

Then there is the core drawing code, because the code is too long, a part is posted here

def draw_bdd(t):

    # reset
    t.penup()
    t.home()
    t.clear()

    # adjust the speed
    t.speed(30)

    # left hand
    t.goto(177, 112)

    t.pencolor("lightgray")

    t.pensize(3)

    t.fillcolor("white")

    t.begin_fill()

    t.pendown()

    t.setheading(80)

    t.circle(-45, 200)

    t.circle(-300, 23)

    t.end_fill()

Check complete code to draw.py

Finally, use pytxui - python GUI component library to put two more beautiful buttons for repainting and downloading features.

The download feature uses Pillow's ImageGrab screenshot function, which can save the drawing result of the Bing Dwen Dwen on Canvas as a PNG image.

Core screenshot save code

from PIL import ImageGrab
import os

def save_image(root, widget):
    root.update()
    x=root.winfo_rootx()+widget.winfo_x()
    y=root.winfo_rooty()+widget.winfo_y()

    x1=x+widget.winfo_width()
    y1=y+widget.winfo_height()
    offset = 4
    ImageGrab.grab().crop((x + offset,y + offset,x1 - offset,y1 - offset)).save(os.getcwd() + "\\bdd.png", "PNG")

Source Code

pytxui-draw_bdd

Conclusion

Above, we have learned Python-related knowledge in combination with the current fire of Bing Dwen Dwen

  • Use turtle and tkinter to construct GUI
  • How to construct a beautiful GUI button component
  • How to download tkinter Canvas as image

There are still many shortcomings. You are welcome to give the author more suggestions and ideas to learn and improve together.

Reference

Comments