Skip to main content
added 14 characters in body
Source Link

This question was old, but I found it in a search because it was related to an issue I am having. My attempt at fixing this was to try to implement "double-buffering". On my computer, my version of code fixes the clipping that occurs causing 1/4 of the circle to be a square. It's still not as smooth as I'd like though.

from tkinter import *
import time

WIDTH = 800
HEIGHT = 500
SIZE = 50
tk = Tk()
canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="blue")
canvas.pack()
color = 'black'

class Ball:
    def __init__(self, tag):
        self.shape = canvas.create_oval(0, 0, SIZE, SIZE, fill=color, tags=tag)
        self.speedx = 9 # changed from 3 to 9
        self.speedy = 9 # changed from 3 to 9
        self.active = True

    def ball_update(self):
        canvas.move(self.shape, self.speedx, self.speedy)
        pos = canvas.coords(self.shape)
        if pos[2] >= WIDTH or pos[0] <= 0:
            self.speedx *= -1
        if pos[3] >= HEIGHT or pos[1] <= 0:
            self.speedy *= -1

defglobal cycle():switcher
    bgswitcher = canvas.create_rectangle(0, 0, WIDTH+1, HEIGHT+1, fill="gray", tags="bg")True
    ball =def Ballcycle("ball"):
    ball.ball_update()
 global switcher
  ball2 = Ballcanvas.tag_raise("ball2""bg")
    whileif Trueswitcher:
        canvas.tag_raise("bg")
        ball2.ball_update()
        ball2.ball_update()
        canvas.tag_raise("ball")
    else:
    tk    ball.updateball_update()
        tkball.afterball_update(40)
        canvas.tag_raise("bg""ball2")
        balltk.ball_updateupdate_idletasks()
     switcher = not ball.ball_update()switcher
     tk.after(40, cycle)

bg = canvas.tag_raisecreate_rectangle("ball2")
 0, 0, WIDTH+1, HEIGHT+1, fill="gray", tags="bg")
ball = tkBall("ball")
ball.updateball_update()
      ball2 = tk.afterBall(40"ball2")
 

tk.after(0, cycle)
tk.mainloop()

This question was old, but I found it in a search because it was related to an issue I am having. My attempt at fixing this was to try to implement "double-buffering". On my computer, my version of code fixes the clipping that occurs causing 1/4 of the circle to be a square. It's still not as smooth as I'd like though.

from tkinter import *
import time

WIDTH = 800
HEIGHT = 500
SIZE = 50
tk = Tk()
canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="blue")
canvas.pack()
color = 'black'

class Ball:
    def __init__(self, tag):
        self.shape = canvas.create_oval(0, 0, SIZE, SIZE, fill=color, tags=tag)
        self.speedx = 9 # changed from 3 to 9
        self.speedy = 9 # changed from 3 to 9
        self.active = True

    def ball_update(self):
        canvas.move(self.shape, self.speedx, self.speedy)
        pos = canvas.coords(self.shape)
        if pos[2] >= WIDTH or pos[0] <= 0:
            self.speedx *= -1
        if pos[3] >= HEIGHT or pos[1] <= 0:
            self.speedy *= -1

def cycle():
    bg = canvas.create_rectangle(0, 0, WIDTH+1, HEIGHT+1, fill="gray", tags="bg")
    ball = Ball("ball")
    ball.ball_update()
    ball2 = Ball("ball2")
    while True:
        canvas.tag_raise("bg")
        ball2.ball_update()
        ball2.ball_update()
        canvas.tag_raise("ball")
        tk.update()
        tk.after(40)
        canvas.tag_raise("bg")
        ball.ball_update()
        ball.ball_update()
        canvas.tag_raise("ball2")
        tk.update()
        tk.after(40)
 

tk.after(0, cycle)
tk.mainloop()

This question was old, but I found it in a search because it was related to an issue I am having. My attempt at fixing this was to try to implement "double-buffering". On my computer, my version of code fixes the clipping that occurs causing 1/4 of the circle to be a square. It's still not as smooth as I'd like though.

from tkinter import *
import time

WIDTH = 800
HEIGHT = 500
SIZE = 50
tk = Tk()
canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="blue")
canvas.pack()
color = 'black'

class Ball:
    def __init__(self, tag):
        self.shape = canvas.create_oval(0, 0, SIZE, SIZE, fill=color, tags=tag)
        self.speedx = 9 # changed from 3 to 9
        self.speedy = 9 # changed from 3 to 9
        self.active = True

    def ball_update(self):
        canvas.move(self.shape, self.speedx, self.speedy)
        pos = canvas.coords(self.shape)
        if pos[2] >= WIDTH or pos[0] <= 0:
            self.speedx *= -1
        if pos[3] >= HEIGHT or pos[1] <= 0:
            self.speedy *= -1

global switcher
switcher = True
def cycle():
    global switcher
    canvas.tag_raise("bg")
    if switcher:
        ball2.ball_update()
        ball2.ball_update()
        canvas.tag_raise("ball")
    else:
        ball.ball_update()
        ball.ball_update()
        canvas.tag_raise("ball2")
    tk.update_idletasks()
    switcher = not switcher
    tk.after(40, cycle)

bg = canvas.create_rectangle(0, 0, WIDTH+1, HEIGHT+1, fill="gray", tags="bg")
ball = Ball("ball")
ball.ball_update()
ball2 = Ball("ball2")

tk.after(0, cycle)
tk.mainloop()
Source Link

This question was old, but I found it in a search because it was related to an issue I am having. My attempt at fixing this was to try to implement "double-buffering". On my computer, my version of code fixes the clipping that occurs causing 1/4 of the circle to be a square. It's still not as smooth as I'd like though.

from tkinter import *
import time

WIDTH = 800
HEIGHT = 500
SIZE = 50
tk = Tk()
canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="blue")
canvas.pack()
color = 'black'

class Ball:
    def __init__(self, tag):
        self.shape = canvas.create_oval(0, 0, SIZE, SIZE, fill=color, tags=tag)
        self.speedx = 9 # changed from 3 to 9
        self.speedy = 9 # changed from 3 to 9
        self.active = True

    def ball_update(self):
        canvas.move(self.shape, self.speedx, self.speedy)
        pos = canvas.coords(self.shape)
        if pos[2] >= WIDTH or pos[0] <= 0:
            self.speedx *= -1
        if pos[3] >= HEIGHT or pos[1] <= 0:
            self.speedy *= -1

def cycle():
    bg = canvas.create_rectangle(0, 0, WIDTH+1, HEIGHT+1, fill="gray", tags="bg")
    ball = Ball("ball")
    ball.ball_update()
    ball2 = Ball("ball2")
    while True:
        canvas.tag_raise("bg")
        ball2.ball_update()
        ball2.ball_update()
        canvas.tag_raise("ball")
        tk.update()
        tk.after(40)
        canvas.tag_raise("bg")
        ball.ball_update()
        ball.ball_update()
        canvas.tag_raise("ball2")
        tk.update()
        tk.after(40)


tk.after(0, cycle)
tk.mainloop()