Tkinter animation help?

Post here if you need help with creating a Graphical User Interface in Python.

Tkinter animation help?

Postby dannyxy » Fri Mar 29, 2013 4:45 pm

Hi i'm new here
How can check if an object hits the corners of the screen because i made an animation and it keeps moving forever?
Thanks
Last edited by Yoriz on Fri Mar 29, 2013 5:21 pm, edited 1 time in total.
Reason: Added Tkinter to title and moved topic to gui forum
dannyxy
 
Posts: 18
Joined: Fri Mar 29, 2013 11:26 am

Re: Python animation help?

Postby Yoriz » Fri Mar 29, 2013 5:13 pm

That would depend on what your using to do the animating, but you would need to check the position of objects are inside the screen area and if they not decide what to do with them bounce there co-ord or remove them etc.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 833
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Python animation help?

Postby dannyxy » Fri Mar 29, 2013 5:18 pm

I am using Tkinter
dannyxy
 
Posts: 18
Joined: Fri Mar 29, 2013 11:26 am

Re: Tkinter animation help?

Postby dannyxy » Sat Mar 30, 2013 2:15 pm

Thanks its working now
dannyxy
 
Posts: 18
Joined: Fri Mar 29, 2013 11:26 am

Re: Tkinter animation help?

Postby wuf » Sun Mar 31, 2013 11:16 pm

Hi dannyxy

Here a tkinter animation demo to try out:
Code: Select all
import random
try:
    #~~ For Python 2.x
    import Tkinter as tk
except ImportError:
    #~~ For Python 3.x
    import tkinter as tk

APP_WIN_XPOS = 50
APP_WIN_YPOS = 50

PLAIN_WIDTH = 400
PLAIN_HEIGHT = 400

OBJ_WIDTH =100
OBJ_HEIGHT = 100
OBJ_TAG = 'rect'

class AnimationObject(object):
    STEP_TABLE = (2, 3, 4, 5)
    TIME_TABLE = (20, 30, 40, 50, 60, 70, 80, 90, 100)
    COLOR_TABLE = ('yellow', 'green', 'blue', 'red')
    TAG_PREFIX = 'rect-'
    object_count = 0
   
    def __init__(self, plain):
        self.plain = plain
        self.xpos = 0
        self.ypos = random.randint(0, PLAIN_HEIGHT-OBJ_HEIGHT)
        self.xstep = random.choice(AnimationObject.STEP_TABLE)
        self.ystep = random.choice(AnimationObject.STEP_TABLE)
        self.step_time = random.choice(self.TIME_TABLE)
        self.color = random.choice(self.COLOR_TABLE)
        self.tag = '{0}{1}'.format(self.TAG_PREFIX,
            AnimationObject.object_count)
        AnimationObject.object_count += 1

        self.plain.create_oval(self.xpos, self.ypos, self.xpos+OBJ_WIDTH,
            self.ypos+OBJ_HEIGHT, width=1, fill=self.color, tags=self.tag)
       
        self.plain.create_text(self.xpos+OBJ_WIDTH/2, self.ypos+OBJ_HEIGHT/2,
            text=AnimationObject.object_count, font=('Helvetica',16, 'bold'),
            tags=self.tag)
               
        self.animator()
       
    def animator(self):
        self.move_object()
        self.plain.after(self.step_time, self.animator)

    def move_object(self):
        """Sprite Move and bounce check"""
        if self.xpos <= 0:
            self.xstep = random.choice(AnimationObject.STEP_TABLE)
        if self.xpos+OBJ_WIDTH >= PLAIN_WIDTH:
            self.xstep = -random.choice(AnimationObject.STEP_TABLE)   
        if self.ypos <= 0:
            self.ystep = random.choice(AnimationObject.STEP_TABLE)
        if self.ypos+OBJ_HEIGHT >= PLAIN_HEIGHT:
            self.ystep = -random.choice(AnimationObject.STEP_TABLE)
       
        self.plain.move(self.tag, self.xstep, self.ystep)
        self.xpos, self.ypos = self.plain.coords(self.tag)[:2]
           
class App(object):
   
    def __init__(self, title=''):
        self.win = tk.Tk()
        self.win.title(title)

        self.win.geometry('+{0}+{1}'.format(APP_WIN_XPOS, APP_WIN_YPOS))
        self.win.protocol("WM_DELETE_WINDOW", self.close)
           
        self.plain = tk.Canvas(self.win, width=PLAIN_WIDTH, height=PLAIN_HEIGHT,
            highlightthickness=0, bd=1, bg='steelblue')
        self.plain.pack()
       
        self.ani_objects = [AnimationObject(self.plain) for obj in range(20)]
   
    def run(self):
        self.win.mainloop()
   
    def close(self):
        self.win.destroy()
           
App("Tk Animation").run()


wuf ;)
wuf
 
Posts: 38
Joined: Fri Feb 08, 2013 6:42 am


Return to GUI

Who is online

Users browsing this forum: Google [Bot] and 2 guests