DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Drawing a moving circle with Python Tkinter + good GUI tutorial (http://www.daniweb.com/forums/thread106935.html)

Valmian Jan 30th, 2008 2:29 pm
Drawing a moving circle with Python Tkinter + good GUI tutorial
 
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:

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

G-Do Jan 30th, 2008 3:56 pm
Re: Drawing a moving circle with Python Tkinter + good GUI tutorial
 
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!

jrcagle Jan 30th, 2008 11:26 pm
Re: Drawing a moving circle with Python Tkinter + good GUI tutorial
 
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.

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()

Valmian Feb 1st, 2008 1:31 pm
Re: Drawing a moving circle with Python Tkinter + good GUI tutorial
 
All right, thank you.

Ilya


All times are GMT -4. The time now is 5:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC