python Tkinter multithreading
Your program is not very difficult to modify so that it uses the GUI main loop and after
method calls. The code in the main
function should probably be encapsulated in a class that inherits from tkinter.Frame
, but the following example is complete and demonstrates one possible solution:
#! /usr/bin/env python3import tkinterFPS = 25WIDTH, HEIGHT = 500, 500def main(): tkinter.NoDefaultRoot() root = tkinter.Tk() root.resizable(False, False) canvas = tkinter.Canvas(root, bg='grey', width=WIDTH, height=HEIGHT) canvas.grid() balls = ( Ball(canvas, 10, 10, 40, 40, 'black'), Ball(canvas, 50, 50, 80, 80, 'red') ) root.after(1000 // FPS, update_balls, root, balls) root.mainloop()def update_balls(root, balls): root.after(1000 // FPS, update_balls, root, balls) for ball in balls: ball.update()class Ball: def __init__(self, canvas, x1, y1, x2, y2, color): self.__canvas = canvas self.__x_velocity = 9 self.__y_velocity = 5 self.__id = canvas.create_oval(x1, y1, x2, y2, fill=color) def update(self): self.__canvas.move(self.__id, self.__x_velocity, self.__y_velocity) x1, y1, x2, y2 = self.__canvas.coords(self.__id) if x1 < 0 or x2 > WIDTH: self.__x_velocity *= -1 if y1 < 0 or y2 > HEIGHT: self.__y_velocity *= -1if __name__ == '__main__': main()