• Как в python tkinter повернуть фигуру или картинку?

    @danila_hlopcev
    from tkinter import *
    import math

    def left(event):
    global line1, line2, line3, line4, alpha, d, pos1, pos2, pos3, pos4
    for i in range(4):
    alpha[i] -= 1
    canvas.delete(line1, line2, line3, line4)
    pos1 = [100+d*math.cos(math.radians(alpha[0])),
    100+d*math.sin(math.radians(alpha[0]))]
    pos2 = [100+d*math.cos(math.radians(alpha[1])),
    100+d*math.sin(math.radians(alpha[1]))]
    pos3 = [100+d*math.cos(math.radians(alpha[2])),
    100+d*math.sin(math.radians(alpha[2]))]
    pos4 =[100+d*math.cos(math.radians(alpha[3])),
    100+d*math.sin(math.radians(alpha[3]))]
    line1 = canvas.create_line(pos1[0], pos1[1], pos2[0], pos2[1])
    line2 = canvas.create_line(pos2[0], pos2[1], pos3[0], pos3[1])
    line3 = canvas.create_line(pos3[0], pos3[1], pos4[0], pos4[1])
    line4 = canvas.create_line(pos4[0], pos4[1], pos1[0], pos1[1])

    def right(event):
    global line1, line2, line3, line4, alpha, d, pos1, pos2, pos3, pos4
    for i in range(4):
    alpha[i] += 1
    canvas.delete(line1, line2, line3, line4)
    pos1 = [100+d*math.cos(math.radians(alpha[0])),
    100+d*math.sin(math.radians(alpha[0]))]
    pos2 = [100+d*math.cos(math.radians(alpha[1])),
    100+d*math.sin(math.radians(alpha[1]))]
    pos3 = [100+d*math.cos(math.radians(alpha[2])),
    100+d*math.sin(math.radians(alpha[2]))]
    pos4 =[100+d*math.cos(math.radians(alpha[3])),
    100+d*math.sin(math.radians(alpha[3]))]
    line1 = canvas.create_line(pos1[0], pos1[1], pos2[0], pos2[1])
    line2 = canvas.create_line(pos2[0], pos2[1], pos3[0], pos3[1])
    line3 = canvas.create_line(pos3[0], pos3[1], pos4[0], pos4[1])
    line4 = canvas.create_line(pos4[0], pos4[1], pos1[0], pos1[1])



    root = Tk()
    canvas = Canvas(root, width=200, height=200)
    canvas.pack()
    alpha = [60, 120, 240, 300]
    d=50
    pos1 = [100+d*math.cos(math.radians(alpha[0])),
    100+d*math.sin(math.radians(alpha[0]))]
    pos2 = [100+d*math.cos(math.radians(alpha[1])),
    100+d*math.sin(math.radians(alpha[1]))]
    pos3 = [100+d*math.cos(math.radians(alpha[2])),
    100+d*math.sin(math.radians(alpha[2]))]
    pos4 =[100+d*math.cos(math.radians(alpha[3])),
    100+d*math.sin(math.radians(alpha[3]))]

    line1 = canvas.create_line(pos1[0], pos1[1], pos2[0], pos2[1])
    line2 = canvas.create_line(pos2[0], pos2[1], pos3[0], pos3[1])
    line3 = canvas.create_line(pos3[0], pos3[1], pos4[0], pos4[1])
    line4 = canvas.create_line(pos4[0], pos4[1], pos1[0], pos1[1])

    canvas.focus_set()
    canvas.bind("", right)
    canvas.bind("", left)
    root.mainloop()
    Ответ написан
    Комментировать