Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple 2d surface with arrow in python?

I have not previously worked in python. I need to do a really simple 2d surface where i can place an arrow and then be able to change the position and the angle of the arrow.

I started to build something like it in tkinter, but as I understand it you are not able to rotate images. To my understanding only polygons can be rotated. It seems a little overly complicated to draw an arrow as a polygon.

Are there some other tools that are more suitable for this kinds of simple stuff?

Thanks

like image 327
Chippen Avatar asked Sep 21 '12 11:09

Chippen


1 Answers

Tkinter is an excellent choice for such a simple task. You almost certainly already have it installed, and the Canvas widget is remarkably powerful. It has built-in facilities to draw lines that have an arrow at the end, and rotation is very straight-forward.

Don't let "common knowledge" about Tkinter sway you -- it is a modern, stable, and extremely easy to use toolkit. You can't create the next photoshop or iMovie with it, but for most people and for most apps it is a very solid, pragmatic choice.

Here is a quick and dirty example:

import Tkinter as tk
import math

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.canvas = tk.Canvas(self, width=400, height=400)
        self.canvas.pack(side="top", fill="both", expand=True)
        self.canvas.create_line(200,200, 200,200, tags=("line",), arrow="last")
        self.rotate()

    def rotate(self, angle=0):
        '''Animation loop to rotate the line by 10 degrees every 100 ms'''
        a = math.radians(angle)
        r = 50
        x0, y0 = (200,200)
        x1 = x0 + r*math.cos(a)
        y1 = y0 + r*math.sin(a)
        x2 = x0 + -r*math.cos(a)
        y2 = y0 + -r*math.sin(a)
        self.canvas.coords("line", x1,y1,x2,y2)
        self.after(100, lambda angle=angle+10: self.rotate(angle))

app = ExampleApp()
app.mainloop()
like image 185
Bryan Oakley Avatar answered Sep 26 '22 21:09

Bryan Oakley