Drawing a moving circle with Python Tkinter + good GUI tutorial

Thread Solved
Reply

Join Date: Sep 2003
Posts: 81
Reputation: Valmian is an unknown quantity at this point 
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Drawing a moving circle with Python Tkinter + good GUI tutorial

 
0
  #1
Jan 30th, 2008
Hello,

I have just started playing around a bit with Python/Tkinter. What I'd like to happen is to draw a circle and then have the circle change its coordinate. The way I was thinking is to have some integer i to be defined 0 and then be updated every frame (ie i+=1), with the drawing algorithm modified appropriately. However, since mainloop() seems to act as a sort of global while(1) loop, this doesn't seem possible. What I have now:

  1. from Tkinter import *
  2.  
  3. root = Tk()
  4. def drawcircle(canv,x,y,rad):
  5. canv.create_oval(x-rad,y-rad,x+rad,y+rad,width=0,fill='blue')
  6.  
  7. canvas = Canvas(width=600, height=200, bg='white')
  8. canvas.pack(expand=YES, fill=BOTH)
  9.  
  10. text = canvas.create_text(50,10, text="tk test")
  11.  
  12. #i'd like to recalculate these coordinates every frame
  13. circ1=drawcircle(canvas,100,100,20)
  14. circ2=drawcircle(canvas,500,100,20)
  15.  
  16. root.mainloop()

So, how do I go about doing this?

Another question, could anyone suggest a good Tkinter/Python tutorial? What I need is maximally simple and relatively short tutorial that will allow me to make *simple* 2D simulations (ie, my goal for a first Python algorithm is a 1D molecular dynamics simulation using Lennard-Jones potential).

Thank you very much in advance,

Ilya
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: Drawing a moving circle with Python Tkinter + good GUI tutorial

 
0
  #2
Jan 30th, 2008
Hi Ilya,

If you want to do interactive simulations, I think you might be better off checking out pygame, which is designed for games but can easily be adapted for this kind of thing. I have written simple 2D epithelial tumor growth simulations in pygame - I can't imagine how you could accomplish such a thing in Tkinter, short of setting up a separate thread to mess with the Tkinter root or canvas, and from what I remember that introduces all kinds of synchronization issues.

The pygame tutorials and API documentation are also available on the site I linked.

Hope this helps!
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 149
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Drawing a moving circle with Python Tkinter + good GUI tutorial

 
0
  #3
Jan 30th, 2008
I agree with G-Do's advice, but if you are stuck using Tkinter, then you want to think like this: events instead of timeline.

In other words, something has to trigger your application to move the circle.

Here's a version that fixes a bug and also moves the circles on every mouse click.

  1. from Tkinter import *
  2.  
  3. root = Tk()
  4.  
  5. # All of this would do better as a subclass of Canvas with specialize methods
  6.  
  7. def drawcircle(canv,x,y,rad):
  8. # changed this to return the ID
  9. return canv.create_oval(x-rad,y-rad,x+rad,y+rad,width=0,fill='blue')
  10.  
  11. def movecircle(canv, cir):
  12. canv.move(cir,-1,-1)
  13.  
  14. def callback(event):
  15. movecircle(canvas, circ1)
  16. movecircle(canvas, circ2)
  17.  
  18. canvas = Canvas(width=600, height=200, bg='white')
  19. canvas.bind("<Button-1>", callback)
  20. canvas.pack(expand=YES, fill=BOTH)
  21.  
  22.  
  23. text = canvas.create_text(50,10, text="tk test")
  24.  
  25. #i'd like to recalculate these coordinates every frame
  26. circ1=drawcircle(canvas,100,100,20)
  27. circ2=drawcircle(canvas,500,100,20)
  28.  
  29. root.mainloop()
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 81
Reputation: Valmian is an unknown quantity at this point 
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Re: Drawing a moving circle with Python Tkinter + good GUI tutorial

 
0
  #4
Feb 1st, 2008
All right, thank you.

Ilya
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC