3

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.

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?

No Bounds
  • 43
  • 6
  • for more clarification, (200, 200) is the center of the circle. I need help for figuring out the coordinates to make it on each slice – No Bounds Jun 18 '19 at 15:33
  • that would make sense, but how would it be able to change the coordinates around the circle? – No Bounds Jun 18 '19 at 15:45

2 Answers2

5

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] 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()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • this is great; but i still don't understand how to make it like an actual spinning wheel design. Check the link that I added to my description above – No Bounds Jun 18 '19 at 16:58
3

You can rotate the text. Tcl 8.6 added this feature.

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

If you combine these 2 answers you can get what you want, where you change the rotation AND location of each text.

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()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Novel
  • 13,406
  • 2
  • 25
  • 41