【发布时间】:2019-04-25 05:37:00
【问题描述】:
这是我使用 create_lines 在 python tkinter 画布中绘制三角形和正方形的代码,我将如何使用 create_lines 创建五边形和六边形?
注意:对于五边形和六边形,长度和宽度是指包含形状的正方形的总面积,而不是边的宽度和长度。
self.x, self.y = 50, 50
def triangle(self):
width = self.width.get()
length = self.length.get()
color = self.color_select.get()
self.canvas.create_line(self.x, self.y, (self.x + (int(length) / 2)), (self.y + int(length)), fill = color)
self.canvas.create_line(self.x, self.y, (self.x - (int(length) / 2)), (self.y + int(length)), fill = color)
self.canvas.create_line((self.x - (int(length) / 2)), (self.y + int(length)), (self.x + (int(length) / 2)), (self.y + int(length)), fill = color)
self.x += 50
def square(self):
width = self.width.get()
length = self.length.get()
color = self.color_select.get()
self.canvas.create_line(self.x, self.y, self.x + int(width), self.y, fill = color)
self.canvas.create_line(self.x, self.y, self.x, self.y + int(length), fill = color)
self.y += int(length)
self.canvas.create_line(self.x, self.y, self.x + int(width), self.y, fill = color)
self.x += int(width)
self.canvas.create_line(self.x, self.y, self.x, self.y - int(length), fill = color)
self.y -= int(length)
self.x += 50
def pentagon(self):
width = self.width.get()
length = self.length.get()
color = self.color_select.get()
def hexagon(self):
width = self.width.get()
length = self.length.get()
color = self.color_select.get()
【问题讨论】:
-
您必须使用
math.sin()、math.cos()或math.tan()来计算一些距离。 -
添加到 furas 答案,使用create_polygon 并在画布上创建一个实体,而不是用多行填充它
标签: python python-3.x tkinter geometry tkinter-canvas