这是我第一次利用四舍五入,因此,如果格式错,我会抱歉。
I m trying to make this function do 2 things. First, I m drawing a wheel with an amount of fields equal to the amount of descriptions given, with field having a different hue of a given color. Then, the text for each option is given via the array of strings, and this text should be put inside each of the fields on the wheel. I have figured out where to place the text, but I can t seem to get the rotation down.
def drawWheel(self):
"""
Draws the GUI wheel with a random selection of colors. Uses amount of string objects as amount of fields
"""
anglePerSection = 360 / self.sections
x0, y0, x1, y1 = 30, 30, 570, 570
radius = 250
centerX, centerY = 300, 300
self.canvas.create_oval(x0, y0, x1, y1, fill="white")
for i, description in enumerate(self.descriptions):
startAngle = anglePerSection * i + self.angle
txtStartAngle = anglePerSection * i - self.angle
self.canvas.create_arc(x0, y0, x1, y1,
start=startAngle,
extent=anglePerSection,
fill=self.getColorBrightness(self.color, i),
outline="black")
midpointAngle = (txtStartAngle + (anglePerSection / 2)) % 360
angle_rad = np.radians(midpointAngle)
textX = centerX + radius * np.cos(angle_rad)
textY = centerY + radius * np.sin(angle_rad)
self.canvas.create_text(textX, textY, text=description, font=("Arial", 14), fill="white", angle=anglePerSection * i + startAngle)
self.canvas.create_oval(centerX - 85, centerY - 85, centerX + 85, centerY + 85, fill="white")
采用<代码>自行.canvas.create_text(textX, textY, text=description, font=(Arial), 14),填充“white”, 角=midpointAngle-90 % 360), 角逐段应当计算案文所需的轮换。 然而,在我一生中,我无法看到正确的轮换。
I ve tried a few different calculations, mainly using anglePerSection * i + startAngle
with no luck. The good news is, the text rotation angle stays consistent with this calculation, so I m thinking I just need to add some constant value to orient all the numbers correctly. They should of course be oriented so the text fills the field from the edge to the center.
Edit: Minimum reproduable example
import tkinter as tk
import numpy as np
if __name__ == __main__ :
root = tk.Tk()
sections = 21
numberList = [str(i) for i in range(1, sections + 1)]
canvas = tk.Canvas(root, width=600, height=600)
canvas.pack()
anglePerSection = 360 / sections
x0, y0, x1, y1 = 30, 30, 570, 570
radius = 250
centerX, centerY = 300, 300
canvas.create_oval(x0, y0, x1, y1, fill="white")
for i, number in enumerate(numberList):
startAngle = anglePerSection * i
canvas.create_arc(x0, y0, x1, y1,
start=startAngle,
extent=anglePerSection,
fill="white",
outline="black")
midpointAngle = (startAngle + anglePerSection / 2) % 360
angle_rad = np.radians(midpointAngle)
textX = centerX + radius * np.cos(angle_rad)
textY = centerY + radius * np.sin(angle_rad)
canvas.create_text(textX, textY, text=number, font=("Arial", 14), fill="black", angle=anglePerSection * i + startAngle)
root.mainloop()
该法典产生了一个有多个领域的圈子。 离“一米”方向最接近11。 这里还有一点要补充,我也很难把人数的下半点推到底,但我稍后将对此感到关切。