利用tkinter模块绘制任意的5个颜色不同积木图案
时间: 2023-08-16 19:10:13 浏览: 106
tkinter模块的使用
好的,下面是一个利用 tkinter 模块绘制五个不同颜色的积木图案的示例代码:
```python
import tkinter as tk
# 创建主窗口
root = tk.Tk()
# 设置窗口标题
root.title("五个颜色不同积木图案")
# 设置画布尺寸
canvas_width = 500
canvas_height = 500
# 创建画布
canvas = tk.Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()
# 绘制红色矩形积木
rect_x1, rect_y1 = 50, 50
rect_x2, rect_y2 = 150, 150
canvas.create_rectangle(rect_x1, rect_y1, rect_x2, rect_y2, fill="red")
# 绘制黄色梯形积木
trapezoid_x1, trapezoid_y1 = 200, 50
trapezoid_x2, trapezoid_y2 = 300, 50
trapezoid_x3, trapezoid_y3 = 350, 150
trapezoid_x4, trapezoid_y4 = 150, 150
canvas.create_polygon(trapezoid_x1, trapezoid_y1, trapezoid_x2, trapezoid_y2, trapezoid_x3, trapezoid_y3, trapezoid_x4, trapezoid_y4, fill="yellow")
# 绘制绿色圆形积木
circle_x, circle_y = 250, 250
circle_r = 50
canvas.create_oval(circle_x-circle_r, circle_y-circle_r, circle_x+circle_r, circle_y+circle_r, fill="green")
# 绘制蓝色三角形积木
triangle_x1, triangle_y1 = 400, 100
triangle_x2, triangle_y2 = 450, 200
triangle_x3, triangle_y3 = 350, 200
canvas.create_polygon(triangle_x1, triangle_y1, triangle_x2, triangle_y2, triangle_x3, triangle_y3, fill="blue")
# 绘制紫色平行四边形积木
parallelogram_x1, parallelogram_y1 = 200, 300
parallelogram_x2, parallelogram_y2 = 300, 300
parallelogram_x3, parallelogram_y3 = 250, 400
parallelogram_x4, parallelogram_y4 = 150, 400
canvas.create_polygon(parallelogram_x1, parallelogram_y1, parallelogram_x2, parallelogram_y2, parallelogram_x3, parallelogram_y3, parallelogram_x4, parallelogram_y4, fill="#800080")
# 进入消息循环
root.mainloop()
```
这段代码可以绘制五个不同颜色的积木图案,包括红色矩形积木、黄色梯形积木、绿色圆形积木、蓝色三角形积木和紫色平行四边形积木。你可以根据自己的需要修改图案的形状、颜色和位置。
阅读全文