This is my first time using stack overflow, so I apologize if formatting is wrong
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")
I'm placing the text using the self.canvas.create_text(textX, textY, text=description, font=("Arial", 14), fill="white", angle=midpointAngle-90 % 360)
where the angle param should calculate the needed rotation of the text. I cannot for the life of me figure out the correct rotation however.
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 reproducable 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()
This code produces a circle with numbered fields. The closest number to the orientation I'm looking for is 11. Another thing to add here, I am also having trouble drawing the bottom half of the numbers, but I will concern myself with this later
You have calculated textY
wrong, it should be:
textY = centerY - radius * np.sin(angle_rad)
because y-coordinate of a canvas goes from top to bottom.
Also the value of midpointAngle
is what you want on the angle
option of the text item:
canvas.create_text(textX, textY, text=number, font=("Arial", 14),
fill="black", angle=midpointAngle)
Output:
Or angle=midpointAngle-90
: