1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
from tkinter import *
def avance(a,b):
global x1, y1
x1, y1 = x1+a, y1+b
can1.coords(oval1, x1, y1, x1+30, y1+30)
def move():
global x1,y1
if x1+30 < 300:
avance(20, 0)
else:
avance(-20, 0)
# programme principal
x1, y1 = 10, 10 # coordonnées initiales
# création du widget maître
fen1 = Tk()
fen1.title("Exercice d'animation avec tkinter")
# création de widgets esclaves
can1 = Canvas(fen1, bg='dark grey', height=300, width=300)
oval1 = can1.create_oval(x1, y1, x1+30, y1+30, width=2, fill='red') # point rouge à déplacer
can1.grid(row=0, column=0, columnspan=2)
Button(fen1, text='Quitter', command=fen1.quit).grid(row=1, column=1)
Button(fen1, text='Avance', command=move).grid(row=1, column=0)
# démarrage du réceptionnaire d'événements
fen1.mainloop() |
Partager