| | |
Drawing a moving circle with Python Tkinter + good GUI tutorial
Thread Solved |
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
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:
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
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:
Python Syntax (Toggle Plain Text)
from Tkinter import * root = Tk() def drawcircle(canv,x,y,rad): canv.create_oval(x-rad,y-rad,x+rad,y+rad,width=0,fill='blue') canvas = Canvas(width=600, height=200, bg='white') canvas.pack(expand=YES, fill=BOTH) text = canvas.create_text(50,10, text="tk test") #i'd like to recalculate these coordinates every frame circ1=drawcircle(canvas,100,100,20) circ2=drawcircle(canvas,500,100,20) 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
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!
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
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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.
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.
Python Syntax (Toggle Plain Text)
from Tkinter import * root = Tk() # All of this would do better as a subclass of Canvas with specialize methods def drawcircle(canv,x,y,rad): # changed this to return the ID return canv.create_oval(x-rad,y-rad,x+rad,y+rad,width=0,fill='blue') def movecircle(canv, cir): canv.move(cir,-1,-1) def callback(event): movecircle(canvas, circ1) movecircle(canvas, circ2) canvas = Canvas(width=600, height=200, bg='white') canvas.bind("<Button-1>", callback) canvas.pack(expand=YES, fill=BOTH) text = canvas.create_text(50,10, text="tk test") #i'd like to recalculate these coordinates every frame circ1=drawcircle(canvas,100,100,20) circ2=drawcircle(canvas,500,100,20) root.mainloop()
![]() |
Other Threads in the Python Forum
- Previous Thread: Vision Egg (installing strange outside libraries)
- Next Thread: wxPython help please?
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui heads homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





