#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Question Tkinter rectangle looping


    Hi, I was wondering how to make a rectangle move constantly to the right? Here's my code:

    from tkinter import *

    master = Tk()
    master.title("Pong")

    w = Canvas(master, width=500, height=500)
    w.pack()

    rect_instance = ""

    left = int(20)
    right = int(40)
    up = int(115)
    down = int(135)


    def task():
    global rect_instance
    global left
    global right
    right = right+1
    left = left+1
    master.after(0, task)
    w.delete(rect_instance)
    rect_instance=w.create_rectangle(right, down, left, up, fill="red")


    task()
    print("done")

    The problem is, it won't open the canvas. When I take out the "master.after(0, task)" line, it opens the canvas, but when I put it back in, it won't open it again. Help!!!!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    Code:
    import tkinter
    import time
    
    master = tkinter.Tk()
    master.title("Pong")
    
    w = tkinter.Canvas(master, width=500, height=500)
    w.pack()
    
    left = 20
    right = 40
    up = 115
    down = 135
    
    rect_instance = w.create_rectangle(left, down, right, up, fill="red")
    
    def task():
        global left,right
        right += 1
        left += 1
        master.after(10, task)		################# changed to 10 millisecond
        w.coords(rect_instance,left, down, right, up)   ##### replace coordinates rather than delete/recreate item
    
    
    task()
    
    master.mainloop()  ################ inserted
    
    print("done")
    Last edited by b49P23TIvg; August 18th, 2013 at 01:48 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    You use Tkinter's move function You might also want to read about item handles and tags on the same page.
    Last edited by dwblas; August 18th, 2013 at 09:20 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Smile


    Originally Posted by b49P23TIvg
    Code:
    import tkinter
    import time
    
    master = tkinter.Tk()
    master.title("Pong")
    
    w = tkinter.Canvas(master, width=500, height=500)
    w.pack()
    
    left = 20
    right = 40
    up = 115
    down = 135
    
    rect_instance = w.create_rectangle(left, down, right, up, fill="red")
    
    def task():
        global left,right
        right += 1
        left += 1
        master.after(10, task)		################# changed to 10 millisecond
        w.coords(rect_instance,left, down, right, up)   ##### replace coordinates rather than delete/recreate item
    
    
    task()
    
    master.mainloop()  ################ inserted
    
    print("done")
    Thank you!!! This did perfectly what I wanted it to, and I can still understand it!
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    tkinter has much functionality, much to learn. I hadn't seen the move function before this. It's a little easier than the coords method.
    Last edited by b49P23TIvg; August 18th, 2013 at 09:50 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo