tkinter库绘制五星红旗
时间: 2024-09-14 21:01:40 浏览: 42
用python画出一幅标准的五星红旗,庆祝国庆节!
tkinter是Python的标准GUI(图形用户界面)库,可以用来创建窗口、按钮、画布等。使用tkinter库绘制五星红旗,你需要按照五星红旗的设计比例和规则,在tkinter的Canvas画布上绘制一个红色的矩形作为旗面,然后根据五颗星的相对位置和大小,使用绘图命令绘制出五颗黄色的星星。以下是一个简单的示例代码:
```python
import tkinter as tk
from math import sin, cos, radians
# 创建窗口
root = tk.Tk()
root.title("五星红旗")
# 创建画布
canvas = tk.Canvas(root, width=300, height=200)
canvas.pack()
# 五星红旗的尺寸比例
flag_width = 30
flag_height = 20
# 画红色背景
canvas.create_rectangle(0, 0, flag_width, flag_height, fill='red')
# 大星星参数
big_star_diameter = 3.5
big_star_center = (5, 3) # 以旗面左下角为(0,0)点计算大星星中心的位置
big_star_points = 5
# 画大星星
big_star_angle = 18
for i in range(big_star_points):
angle = big_star_angle + i * 72
x = big_star_center[0] + big_star_diameter * sin(radians(angle))
y = big_star_center[1] + big_star_diameter * cos(radians(angle))
canvas.create_polygon(x, y, x + sin(radians(54)) * big_star_diameter, y - cos(radians(54)) * big_star_diameter,
x + sin(radians(126)) * big_star_diameter, y - cos(radians(126)) * big_star_diameter,
x + sin(radians(18)) * big_star_diameter, y - cos(radians(18)) * big_star_diameter,
fill='yellow', outline='yellow')
# 四个小星星参数
small_star_diameter = 1
small_star_distance = 1.5
small_stars = [
((10, 1), 162),
((12, 2), 198),
((12, 4), 234),
((10, 5), 270),
]
# 画四个小星星
for center, angle in small_stars:
for i in range(big_star_points):
inner_angle = angle + i * 72 - 36
x = center[0] + small_star_diameter * sin(radians(inner_angle))
y = center[1] + small_star_diameter * cos(radians(inner_angle))
canvas.create_polygon(x, y, x + sin(radians(54)) * small_star_diameter, y - cos(radians(54)) * small_star_diameter,
x + sin(radians(126)) * small_star_diameter, y - cos(radians(126)) * small_star_diameter,
x + sin(radians(18)) * small_star_diameter, y - cos(radians(18)) * small_star_diameter,
fill='yellow', outline='yellow')
# 运行窗口
root.mainloop()
```
这段代码会创建一个300x200像素的窗口,并在其中绘制一个红色背景的五星红旗。请注意,这只是一个简化的示例,实际五星红旗的比例和星星的位置、大小可能需要根据实际规格进行精确计算和调整。
阅读全文