有没有一种方法可以围绕圆形(或圆形)旋转文本? [英] Is there a way to rotate text around (or inside) a circle?

查看:162
本文介绍了有没有一种方法可以围绕圆形(或圆形)旋转文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

典型的纺车我正在用Python tKinter制造纺车.通常,当您旋转轮子时,您会落在轮子上的随机切片上,其中随机选择是切片上显示的文本.我找不到在切片上旋转文本的方法.

typical spinning wheelI am making a spinning wheel in Python tKinter. Usually, when you spin the wheel, you land on a random slice on the wheel, where the random choice is the text displayed on the slice. I am unable to find a way to rotate text on the slices.

我尝试在create_text function中使用angle选项,只有它绕着圆心旋转文本:

I have tried to use the angle option in the create_text function, only it rotates the text around the center of the circle:

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)) 

我想要的预期结果是在纺纱机的每个单独切片上显示文本,但该文本围绕中点旋转.有没有办法做到这一点?

the expected result that I wanted was the text to be displayed on each individual slice of the spinning wheel, but instead it is rotating around the midpoint. Is there a way to not make this happen?

推荐答案

作为一个沿圆形路径旋转的文本块的简单示例,您可以执行以下操作.

As a simple example of a text block rotating along a circular path, you could do something like this.

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()

[Edit]建立在@Novel的基础上,建议tcl8.6添加了rotate功能,这是示例,其中文本沿圆形路径旋转并更改方向:

building on @Novel suggestion that tcl8.6 has added a rotate feature, here is an example where the text rotates along a circular path, and changes orientation:

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()

这篇关于有没有一种方法可以围绕圆形(或圆形)旋转文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆