首页 > 解决方案 > 如何使用python制作一个圆角的乌龟矩形?

问题描述

我想创建一个带圆角的矩形。我正在使用海龟的画布。我有一些工作,我无法找到坐标来制作一个圆形矩形。

在此处输入图像描述

我的文件.txt

S,black,2
L,-250,0,100,0
A,100,90
L,150,150
A,50,90
L,-250,100
A,50,90
L,-20,50
A,100,90
S,green,1
T,Good,60,50,50

程序.py

    **# import turtle & easyshape package**
import turtle
import easyshape as es

pen = turtle.Turtle()

es.pencolor = "blue" #default pencolor as blue
es.penthick = 2    #default penthickness as 2
pen.hideturtle()




while True:
    try:
        # Python code to 
        # demonstrate readlines() 
        # Using readlines() 
        file1 = open('myfile.txt', 'r') 
        Lines = file1.readlines() 

    except FileNotFoundError:
        print("Wrong file or file path")
        break
    else:
        break
  
  
try:
    # Strips the newline character
    # Read & Display content of File in Screen
    for line in Lines: 
        {
            print("{}".format(line.strip()))
        }
   
except ValueError as ve:
    print('which is not a Command number.')

    

#method for pass char by char from file
def get(a,b,c,d,e):
    choice = a
    if choice == "L" or choice == "l":
        #print("Line Section")
        #turtle.onscreenclick(getPosition)
        X1 = int(b)
        x=X1
        Y1 = int(c)
        y=Y1
        X2 = int(d)
        Y2 = int(e)
        es.line(X1, Y1, X2, Y2)
    elif choice == "A" or choice == "a" :
            #print("Line Section")
            r = int(b)
            cz = int(c)
            es.arc(r,cz) #draw arc
    elif choice== "T" or choice == "t":
            #print("Text Section")
            content = b 
            s = int(c)
            X = int(d)
            Y = int(e)
            es.text(content, s, X, Y)
    elif choice == "S" or choice == "s":
      #print("Setting Section")
        es.pencolor = b #Change pencolor
        es.penthick = int(c)    #Change penthickness
        es.setting(es.pencolor,es.penthick)       
    return get

try :
    #setting
    p0 =Lines[0];
    a0,b0,c0 = p0.split(',');
    get(a0,b0,c0,0,0);

    #Line
    p1 = Lines[1];
    # Split the text wherever there's a comma.
    a1,b1,c1,d1,e1 = p1.split(',');
    # Display the results.
    #print(a1);
    get(a1,b1,c1,d1,e1);

    #arc
    p2 = Lines[2];
    a2,b2,c2= p2.split(',');
    #print(a2);
    get(a2,b2,c2,0,0);

    #Line
    p3 = Lines[3];
    a3,b3,c3= p3.split(',');
    #print(a3);
    get(a3,150,90,b3,c3);

    #arc
    p4 = Lines[4];
    a4,b4,c4= p4.split(',');
    #print(a4);
    get(a4,b4,c4,0,0);


    #Line
    p5 = Lines[5];
    a5,b5,c5= p5.split(',');
    #print(a3);
    get(a5,250,200,b5,c5);

    #arc
    p6 = Lines[6];
    a6,b6,c6= p6.split(',');
    #print(a6);
    get(a6,b6,c6,0,0);


    #Line
    p7 = Lines[7];
    a7,b7,c7= p7.split(',');
    #print(a7);
    get(a7,0,150,b7,c7);


    #arc
    p8 = Lines[8];
    a8,b8,c8= p8.split(',');
    #print(a8);
    get(a8,b8,c8,0,0);


    #setting
    ps =Lines[9];
    a9,b9,c9 = ps.split(',');
    get(a9,b9,c9,0,0);


    #text
    txt = Lines[10];
    ta,tb,tc,tx,ty= txt.split(',');
    #print(ta);
    get(ta,tb,tc,tx,ty);

except TypeError as te:
    print (te)
    print (sys.exc_type)

易形状.py

#line(x1, y1, x2, y2)
#text(content, size, x, y)
#arc(radius, angle)
#settings(colour, thickness)

import turtle as t
global pencolor,penthick, x, y

# draw a line from (x1, y1) to (x2, y2)
def line(x1, y1, x2, y2):
    t.hideturtle()
    t.penup()
    t.goto(x1, y1)
    setting(pencolor,penthick)
    t.pendown()
    t.goto(x2, y2)
    t.hideturtle()
    t.dot()
    #t.isvisible()
    #t.done()

def arc(r, a):
    t.circle(r,a)
    setting(pencolor,penthick)
    t.dot()
    #t.home()
    

# write label at location x, y
def text (content, size, x, y):
    FONT = ('Arial', size, 'normal')
    #pen = turtle.Turtle(visible=False)
    t.penup()
    t.goto (x, y)
    t.pendown()
    setting(pencolor,penthick)
    t.write (content, font=FONT)
    t.penup()
    t.hideturtle()
    
def setting(pencolor,penthick):
    #t = turtle.Turtle(visible=False)
    t.pencolor(pencolor)
    t.pensize(penthick)
    #t.hideturtle()
    
    

尽管坐标看起来很复杂,但它只是有条不紊地绕到“矩形”中的每个点,给每个非角点两次。

这是一个可以做的例子:

标签: pythonpython-3.xpython-2.7

解决方案


尝试使用 turtle 重新创建 youtube 徽标时,此处提出了类似的问题(该徽标不是矩形,而是hyperlipse)但是此代码绘制了您正在寻找的内容,并且应该为您提供一种将其实现到您的代码中的方法,请随意查看。


推荐阅读