Python Forum
How to make tkinter object move endlessly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make tkinter object move endlessly
#3
Here is a working example from my toolbox. You will have to remove the x axis portion.
import tkinter as tk

class BounceTest():
    def __init__(self):
        self.running=True
        self.window = tk.Tk()
        self.canvas = tk.Canvas(self.window, width = 400, height = 300)
        self.canvas.grid()

        self.x0 = 10
        self.y0 = 50
        self.x1 = 60
        self.y1 = 100
        self.deltax = 2
        self.deltay = 3
        self.which=self.canvas.create_oval(self.x0, self.y0, self.x1, self.y1,
                                      fill="red", tag='red_ball')

        tk.Button(self.window, text="Stop/Start Ball", bg='yellow',
               command=self.stop_it).grid(row=1, column=0)
        tk.Button(self.window, text="Exit", bg='orange',
               command=self.window.quit).grid(row=2, column=0)
        self.window.after(50, self.move_the_ball)

        self.window.mainloop()

    def move_the_ball(self):
        if self.running:
            self.canvas.move(self.which, self.deltax, self.deltay)
            ## check for ball hitting edges & change direction
            if self.x1 >= 400:
               self.deltax = -2
            if self.x0 < 0:
               self.deltax = 2
            if self.y1 > 290:
                self.deltay = -3
            if self.y0 < 0:
                self.deltay = 3

            ## keep track of the coordinates to tell when
            ## the ball moves off canvas
            self.x0 += self.deltax
            self.x1 += self.deltax
            self.y0 += self.deltay

            self.window.after(25, self.move_the_ball)

    def stop_it(self):
        self.running=not self.running
        if self.running:
            self.move_the_ball()

if __name__ == "__main__":
    BounceTest() 
Reply


Messages In This Thread
RE: How to make tkinter object move endlessly - by woooee - Dec-25-2019, 01:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass an object to a class, then make an object of it and pass again TomasAm 11 4,774 Nov-09-2020, 04:47 PM
Last Post: buran
  Cant move object in game mrsneakys 1 2,294 Mar-25-2019, 01:01 PM
Last Post: ichabod801
  AttributeError: 'tuple' object has no attribute 'move' senfik99 2 4,108 Feb-26-2019, 12:42 PM
Last Post: stullis
  Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 5,647 Mar-05-2018, 03:48 PM
Last Post: mpd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020