Python case 2 - turtle library to draw pentagram V_7.0

Hello everyone, I'm cabbage carrot balls

Life is too short, Python is a song

Draw a five-pointed star
Draw a five-pointed star


project import

Version 7.0: Drawing a pentagram using the canvas control of the tkinter library

Project analysis - clarifying ideas

  1. Analyze the problem: The computational part of the analysis problem
  2. Defining the problem: dividing the input, processing, and output parts of the problem (IPO)
  3. Designing Algorithms: The Heart of the Computational Section

Introduce tkinter
to draw graphics according to the drawing steps
Use the Canvas control in the tkinter library to draw

Note: Python provides several libraries for graphical interface development, and tkinter is one of them. After the introduction of the tkinter library, GUI (Graphic User Interface) applications can be created quickly.

Knowledge point learning – from the ground up


Drawing steps using the Canvas control
in tkinter 1. Import tkinter G is recognizing import tkinter
2. Create the main window and set the canvas size top/root/windows = tkinter.tk() C = tkinter.Canvas(top/root/windows, bg =" blue" , ​​height="300" ,
width="300" )
3. Draw various graphics, such as lines, polygons, arcs, circles, etc.
4. Message main loop (root to pack is equivalent to size, style, position, and then bind an event, mainloop is equivalent to refreshing or starting this event) c.pack() top/root.mainloop()
Canvas syntax format w = canvas ( master , option=value, … )

Analysis of the key steps of drawing
2. Set the properties of the main window, canvas size and background color
top = tkinter.tk()
top.title("Draw a five-pointed star") #Set the title value of the window
C = tkinter.Canvas(top, bg=" blue" , ​​height="300" , width="300" )

Analysis of the key steps of drawing
3. Draw various graphics, such as lines, polygons, arcs, circles, etc.
create_, arc: draw arcs.
create_ rectangle0 : Draw a rectangle.
create.image : draw an image.
create_line() : Draw a line.
create_ polygon : Draw a polygon.
create_ text : Draw text.
create_ window : draws the component.
canvas.creat_polygon(x0,yo,1,/…xn,yn,fill='color")

#Top left point center. x-(r math. sin(2 math. pi/5)), center. y-( r math. cos (2 math. pi/5)),
# Top right point center. x+ (r math .sin(2 math.pi/5)), center y-(r math.cos<(2 math.pi/5)), #lower
left point center.x-(r math.sin(math.pi/5) ), center_ y+(r math.cos(math.pi/5)), #vertex center._X , center._y
-r, #lower
right point center.x+ (r math.sin(math.pi/5)) , center.y+(r math.cos(math.pi/5)), .
Pentagram

Hands-on programming – hands-on, let the program run

reference code


""""
作者:白菜胡萝丸子
版本7.0:使用tkinter库的canvas控件绘制五角星
"""

import tkinter
import math

top = tkinter.Tk()
top.title("绘制五角星")
c = tkinter.Canvas(top, bg="pink", height="300", width="300")

# 使用canvas控件绘制五角星
r = 150
center_x = 150
center_y = 150

polygon = c.create_polygon(
    # 左上点
    center_x - (r*math.sin(2 * math.pi / 5)),
    center_y - (r*math.cos(2 * math.pi / 5)),
    # 右上点
    center_x + (r*math.sin(2 * math.pi / 5)),
    center_y - (r*math.cos(2 * math.pi / 5)),
    # 左下点
    center_x - (r*math.sin(math.pi / 5)),
    center_y + (r*math.cos(math.pi / 5)),
    # 顶点
    center_x,
    center_y - r,
    # 右下点
    center_x + (r*math.sin(math.pi / 5)),
    center_y + (r*math.cos(math.pi / 5)),
    fill="red"
)

# 消息主循环
c.pack()
top.mainloop()

Results of the


Component drawing pentagram


Take it to the next level - let the program evolve a little bit

At present, we have mastered the method of drawing five-pointed stars using turtle library and Canvas control. If we want to draw a standard five-star red flag, how to achieve it?
Please see the next article.

Guess you like

Origin blog.csdn.net/coco264/article/details/123551925