Search code examples
pythoncanvastkinterparticle-filter

Gui for Particlefilter with Python


I'm trying to implement a particle filter and I chose python for it because I kinda like python. By now i have written my gui using tkinter and python 3.4.

I use the tkinter.canvas object to display a map (png image loaded with PIL) and then i create dots for each particle like:

dot = canvas.create_oval(x, y, x + 1, y + 1)

When the robot moves I calculate the new position of each particle with the control command of the robot, the particles position and the particles alignment. To move the particle tkinter.canvas has two methods:

canvas.move()
canvas.coords()

But both methods seem to update the gui immediately which is OK when there are about 100 particles but not if there are 200 - 5000 (what I actually should have in the beginning for the global localization). So my problem is the performance of the gui.

So my actual question is: Is there a way in tkinter to stop the canvas from updating the gui, then change the gui and then update the gui again? Or can you recommend me a module that is better than tkinter for my use-case?


Solution

  • Your observation is incorrect. The canvas is not updated immediately. The oval isn't redrawn until the event loop is able to process events. It is quite possible to update thousands of objects before the canvas is redrawn. Though, the canvas isn't a high performance tool so moving thousands of objects at a high frame rate will be difficult.

    If you are seeing the object being updated immediately it's likely because somewhere in your code you are either calling update, update_idletasks, or you are otherwise allowing the event loop to run.

    The specific answer to your question, then, is to make sure that you don't call update or update_idletasks, or let the event loop process events, until you've changed the coordinates of all of your particles.

    Following is a short example. When it runs, notice that all of the particles move at once in one second intervals. This is because all of the calculations are done before allowing the event loop to redraw the items on the canvas.

    import Tkinter as tk
    import random
    
    class Example(tk.Frame):
        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
    
            self.canvas = tk.Canvas(self, width=500, height=500, background="black")
            self.canvas.pack(fill="both", expand=True)
    
            self.particles = []
            for i in range(1000):
                x = random.randint(1, 499)
                y = random.randint(1, 499)
                particle = self.canvas.create_oval(x,y,x+4,y+4,
                                                   outline="white", fill="white")
                self.particles.append(particle)
    
            self.animate()
    
    
        def animate(self):
            for i, particle in enumerate(self.particles):
                deltay = (2,4,8)[i%3]
                deltax = random.randint(-2,2)
                self.canvas.move(particle, deltax, deltay)
    
            self.after(30, self.animate)
    
    if __name__ == "__main__":
        root = tk.Tk()
        Example(root).pack(fill="both", expand=True)
        root.mainloop()