How to invert a shape in Tkinter

Asked

Viewed 35 times

1

Hi, I wanted to find out how to invert inside the canvas (Tkinter) a Poligono.

from Tkinter import *

Tela_principal = Tk()
Tela_principal.geometry('300x300+10+300')

area_2 = Frame(bg='snow', height=200, width=200, cursor='dotbox')
fundo_area2 = Canvas(area_2, bg='OrangeRed4', height=180, width=180)
area_2.place(x=10, y=10)
fundo_area2.place(x=4, y=4)
x = 5
y = 5


forma = fundo_area2.create_polygon([
        (51 + x, 100 + y), (151 + x, 130 + y),
        (151 + x, 50 + y)],
        fill='gray35', outline='black', tag='personagem')

Tela_principal.mainloop()

that is, in this example the triangle turn to the right. Of course, this would be a simple example. My intention is to reverse polygons with numerous vertices. Anybody got any ideas? Thanks!

1 answer

1

Just change the position x from the tip of the triangle to have its value added to the width of the triangle and subtract the positions x the other coordinates by their width, thus:

forma = fundo_area2.create_polygon(
    [(51 + x + 100, 100 + y), (151 + x - 100, 130 + y), (151 + x - 100, 50 + y)],
    fill='gray35', outline='black', tag='personagem'
    )

If you want, you can also sort of automate this task by creating a variable called largura to calculate coordinates based on the desired direction of the triangle. See this example below:

inicio = 51
largura = 100
direcao = 1 # 0 = esquerda; 1 = direita

forma = fundo_area2.create_polygon(
    [
        (inicio + largura * direcao , 100),
        (inicio + largura - largura * direcao, 130),
        (inicio + largura - largura * direcao, 50)
        ],
    fill='gray35', outline='black', tag='personagem'
    )

Tela_principal.mainloop()

You can also do the same to change the direction on the axis y (down-top), just create a variable of altura and perform the same calculation however at position y.

  • Yes, in a simple example resolves, however a polygon with 30 vertices that locks me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.