有没有办法围绕一个圆圈(或在圆圈内)旋转文本?
Is there a way to rotate text around (or inside) a circle?
typical spinning wheel我正在 Python tKinter 中制作纺车。通常,当您旋转转盘时,您会落在转盘上的随机切片上,其中随机选择是切片上显示的文本。我找不到在切片上旋转文本的方法。
我尝试使用 create_text function
中的角度选项,只是它会围绕圆心旋转文本:
for x in range(len(spinList)):
color = "#"+("%06x"%random.randint(0,16777215))
c.create_arc(xy, start=90+((360/size)*x), extent=(360/size), fill=color, outline='black', width=2)
c.create_text(200, 200, text=spinList[x], angle=90+((180/size)*x))
我想要的预期结果是文本显示在纺车的每个单独切片上,但它围绕中点旋转。有没有办法不让这种情况发生?
作为文本块沿圆形路径旋转的简单示例,您可以这样做。
import math
import tkinter as tk
def rotate(angle=0):
x = math.cos(angle) * 200 + 250
y = math.sin(angle) * 200 + 250
canvas.coords(txt, x, y)
canvas.after(100, rotate, angle+0.1)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
txt = canvas.create_text(250, 50, text='around and around')
rotate()
canvas.pack()
root.mainloop()
[编辑] 基于 tcl8.6
添加了 rotate
功能的 @Novel 建议,这里是 文本沿圆形路径旋转并改变方向的示例:
import math
import tkinter as tk
def rotate(angle1=0, angle2=0):
dx = math.cos(angle1) * 200 + 250
dy = math.sin(angle1) * 200 + 250
canvas.coords(txt, dx, dy)
canvas.itemconfig(txt, angle=angle2)
canvas.after(100, rotate, angle1+0.1, angle2-15)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
txt = canvas.create_text(250, 50, text='around and around')
rotate()
canvas.pack()
root.mainloop()
您可以旋转文字。 Tcl 8.6 添加了此功能。
import tkinter as tk
def rotate(angle=0):
canvas.itemconfig(txt, angle=angle)
canvas.after(100, rotate, angle+5)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
txt = canvas.create_text(250, 250, text='around and around')
rotate()
canvas.pack()
root.mainloop()
如果您结合这 2 个答案,您可以得到您想要的,您可以在其中更改每个文本的旋转和位置。
import math
import tkinter as tk
def rotate(angle=0):
x = math.cos(math.radians(angle)) * 200 + 250
y = math.sin(math.radians(angle)) * 200 + 250
canvas.itemconfig(txt, angle=-angle)
canvas.coords(txt, x, y)
canvas.after(100, rotate, angle+5)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
txt = canvas.create_text(250, 250, text='around and around')
rotate()
canvas.pack()
root.mainloop()
typical spinning wheel我正在 Python tKinter 中制作纺车。通常,当您旋转转盘时,您会落在转盘上的随机切片上,其中随机选择是切片上显示的文本。我找不到在切片上旋转文本的方法。
我尝试使用 create_text function
中的角度选项,只是它会围绕圆心旋转文本:
for x in range(len(spinList)):
color = "#"+("%06x"%random.randint(0,16777215))
c.create_arc(xy, start=90+((360/size)*x), extent=(360/size), fill=color, outline='black', width=2)
c.create_text(200, 200, text=spinList[x], angle=90+((180/size)*x))
我想要的预期结果是文本显示在纺车的每个单独切片上,但它围绕中点旋转。有没有办法不让这种情况发生?
作为文本块沿圆形路径旋转的简单示例,您可以这样做。
import math
import tkinter as tk
def rotate(angle=0):
x = math.cos(angle) * 200 + 250
y = math.sin(angle) * 200 + 250
canvas.coords(txt, x, y)
canvas.after(100, rotate, angle+0.1)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
txt = canvas.create_text(250, 50, text='around and around')
rotate()
canvas.pack()
root.mainloop()
[编辑] 基于 tcl8.6
添加了 rotate
功能的 @Novel 建议,这里是 文本沿圆形路径旋转并改变方向的示例:
import math
import tkinter as tk
def rotate(angle1=0, angle2=0):
dx = math.cos(angle1) * 200 + 250
dy = math.sin(angle1) * 200 + 250
canvas.coords(txt, dx, dy)
canvas.itemconfig(txt, angle=angle2)
canvas.after(100, rotate, angle1+0.1, angle2-15)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
txt = canvas.create_text(250, 50, text='around and around')
rotate()
canvas.pack()
root.mainloop()
您可以旋转文字。 Tcl 8.6 添加了此功能。
import tkinter as tk
def rotate(angle=0):
canvas.itemconfig(txt, angle=angle)
canvas.after(100, rotate, angle+5)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
txt = canvas.create_text(250, 250, text='around and around')
rotate()
canvas.pack()
root.mainloop()
如果您结合这 2 个答案,您可以得到您想要的,您可以在其中更改每个文本的旋转和位置。
import math
import tkinter as tk
def rotate(angle=0):
x = math.cos(math.radians(angle)) * 200 + 250
y = math.sin(math.radians(angle)) * 200 + 250
canvas.itemconfig(txt, angle=-angle)
canvas.coords(txt, x, y)
canvas.after(100, rotate, angle+5)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
txt = canvas.create_text(250, 250, text='around and around')
rotate()
canvas.pack()
root.mainloop()