python turtle画药箱
时间: 2024-05-07 22:14:53 浏览: 118
Python的turtle库是一个很好用的绘图工具,可以用来画各种图形,包括药箱。下面是一个简单的药箱绘制过程:
首先,我们需要导入turtle库,然后创建一个画布和一个画笔对象:
```python
import turtle
canvas = turtle.Screen()
pen = turtle.Turtle()
```
然后,我们可以设置画笔的颜色和线条宽度:
```python
pen.color('red', 'yellow')
pen.width(3)
```
接下来,我们可以开始绘制药箱的边框,这里我们使用turtle的forward和left方法来绘制:
```python
pen.begin_fill()
pen.forward(200)
pen.left(90)
pen.forward(100)
pen.left(90)
pen.forward(200)
pen.left(90)
pen.forward(100)
pen.end_fill()
```
接着,我们绘制药箱的分隔线,同样使用forward和left方法:
```python
pen.penup()
pen.goto(0, 50)
pen.pendown()
pen.forward(200)
```
然后,我们绘制药箱的药品名称,使用turtle的write方法:
```python
pen.penup()
pen.goto(-80, 20)
pen.pendown()
pen.write('药品名称', font=('Arial', 12, 'normal'))
```
最后,我们可以画出药箱的药品数量,同样使用turtle的write方法:
```python
pen.penup()
pen.goto(-80, -10)
pen.pendown()
pen.write('药品数量', font=('Arial', 12, 'normal'))
```
完整的代码如下:
```python
import turtle
canvas = turtle.Screen()
pen = turtle.Turtle()
pen.color('red', 'yellow')
pen.width(3)
pen.begin_fill()
pen.forward(200)
pen.left(90)
pen.forward(100)
pen.left(90)
pen.forward(200)
pen.left(90)
pen.forward(100)
pen.end_fill()
pen.penup()
pen.goto(0, 50)
pen.pendown()
pen.forward(200)
pen.penup()
pen.goto(-80, 20)
pen.pendown()
pen.write('药品名称', font=('Arial', 12, 'normal'))
pen.penup()
pen.goto(-80, -10)
pen.pendown()
pen.write('药品数量', font=('Arial', 12, 'normal'))
turtle.done()
```
阅读全文