我刚开始编程,但在创建此图表时遇到困难。
我试图将图表数字按以下顺序排列 0, 45, 90, 174, 178, 90 -45
我无法将 0 放置在图像的左侧。 如果有人能给我任何关于如何做到这一点的提示,我将提前感激不尽。
我已经尝试过使用断点来可视化角度,但没有得到任何结果。
import tkinter as tk
import math
class CircleArrowApp:
def __init__(self, master):
self.master = master
master.title("HORSTFX")
self.radius = 100 # Sets the initial radius of the circles
# Creating the first circle and its arrow
self.canvas1 = tk.Canvas(master, width=400, height=400, bg='white', highlightbackground='goldenrod', highlightthickness=15)
self.canvas1.pack(side=tk.LEFT, padx=20)
self.angle1 = 0
self.arrow1 = None
self.canvas1.bind("", self.on_canvas1_resize)
# Creation of the second circle and its arrow
self.canvas2 = tk.Canvas(master, width=400, height=400, bg='white', highlightbackground='goldenrod', highlightthickness=15)
self.canvas2.pack(side=tk.LEFT, padx=20)
self.angle2 = 0
self.arrow2 = None
self.canvas2.bind("", self.on_canvas2_resize)
# Creating the buttons
self.button_frame = tk.Frame(master)
self.button_frame.pack(side=tk.BOTTOM, padx=10)`
self.start_button = tk.Button(self.button_frame, text="START", width=10, command=self.start_animation)
self.start_button.pack(side=tk.TOP, pady=10)
self.stop_button = tk.Button(self.button_frame, text="STOP", width=10, command=self.stop_animation)
self.stop_button.pack(side=tk.TOP, pady=10)
self.plus_button = tk.Button(self.button_frame, text="+", width=10, command=self.plus_function)
self.plus_button.pack(side=tk.TOP, pady=10)
self.minus_button = tk.Button(self.button_frame, text="-", width=10, command=self.minus_function)
self.minus_button.pack(side=tk.TOP, pady=10)
self.animating = False
self.animate()
def start_animation(self):
self.animating = True
def stop_animation(self):
self.animating = False
def plus_function(self):
self.radius += 10
self.update_circle_arrows()
def minus_function(self):
if self.radius > 10:
self.radius -= 10
self.update_circle_arrows()
def draw_circle_arrow(self, canvas, angle):
center_x = canvas.winfo_width() // 2
center_y = canvas.winfo_height() // 2
# Set the size of the inner circle relative to the border
inner_radius = self.radius * 0.8
# Draw the circle with white fill and yellow border
canvas.create_oval(center_x - inner_radius, center_y - inner_radius, center_x + inner_radius, center_y + inner_radius, outline='goldenrod', fill='white', width=8)
# Calculate the arrow's endpoints
end_x = center_x + inner_radius * math.cos(angle)
end_y = center_y + inner_radius * math.sin(angle)
# Draw the arrow moving inside the circle
start_x = center_x
start_y = center_y
canvas.create_line(start_x, start_y, end_x, end_y, arrow=tk.LAST, fill='goldenrod', width=6)
if canvas == self.canvas1:
if self.arrow1:
canvas.delete(self.arrow1)
self.arrow1 = canvas.create_line(center_x, center_y, end_x, end_y, arrow=tk.LAST, fill='goldenrod', width=6)
# Adiciona os números no círculo exterior do canvas1
outer_radius = inner_radius + 20
for i in [0, -135, 90, -45, 0, 45, 90, 135, 178]:
angle_rad = math.radians(-i)
num_x = center_x + outer_radius * math.cos(angle_rad)
num_y = center_y + outer_radius * math.sin(angle_rad)
canvas.create_text(num_x, num_y, text=str(i), fill='black')
# Draw the outer circle of canvas1
outer_radius = inner_radius + 2
canvas.create_oval(center_x - outer_radius, center_y - outer_radius, center_x + outer_radius, center_y + outer_radius, outline='', width=2)
elif canvas == self.canvas2:
if self.arrow2:
canvas.delete(self.arrow2)
self.arrow2 = canvas.create_line(center_x, center_y, end_x, end_y, arrow=tk.LAST, fill='goldenrod', width=6)
# Add the numbers to the outer circle
outer_radius = inner_radius + 20
for i in [0, -135, 90, -45, 0, 45, 90, 135, 178]:
angle_rad = math.radians(-i)
num_x = center_x + outer_radius * math.cos(angle_rad)
num_y = center_y + outer_radius * math.sin(angle_rad)
canvas.create_text(num_x, num_y, text=str(i), fill='black')
当您计算
minus
(最终使用 plus
)来翻转它时,您可能需要使用 num_x
而不是 num_y
。
num_x = center_x - radius_outer * math.cos(rad)
或者您可能需要使用
angle+180
来旋转它。
rad = math.radians(-angle+180)
完整的工作示例:
#!/usr/bin/env python3
import math
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(width=200, height=600)
canvas.pack()
all_angles = [0, 45, 90, 135, 180, -45, -90, -135]
# - version 1 - minus, plus -
center_x = 100
center_y = 100
radius_inner = 50
radius_outer = radius_inner + 25
canvas.create_oval(center_x - radius_inner, center_y - radius_inner, center_x + radius_inner, center_y + radius_inner, width=2)
for angle in all_angles:
rad = math.radians(-angle)
num_x = center_x - radius_outer * math.cos(rad)
num_y = center_y + radius_outer * math.sin(rad)
canvas.create_text(num_x, num_y, text=str(angle), anchor='center')
# - version 2 - minus, minus -
center_x = 100
center_y = 300
radius_inner = 50
radius_outer = radius_inner + 25
canvas.create_oval(center_x - radius_inner, center_y - radius_inner, center_x + radius_inner, center_y + radius_inner, width=2)
for angle in all_angles:
rad = math.radians(-angle)
num_x = center_x - radius_outer * math.cos(rad)
num_y = center_y - radius_outer * math.sin(rad)
canvas.create_text(num_x, num_y, text=str(angle), anchor='center')
# - version 3 - rotate -
center_x = 100
center_y = 500
radius_inner = 50
radius_outer = radius_inner + 25
canvas.create_oval(center_x - radius_inner, center_y - radius_inner, center_x + radius_inner, center_y + radius_inner, width=2)
for angle in all_angles:
rad = math.radians(-angle+180)
num_x = center_x + radius_outer * math.cos(rad)
num_y = center_y + radius_outer * math.sin(rad)
canvas.create_text(num_x, num_y, text=str(angle), anchor='center')
root.mainloop()
结果: