Comment mettre une fonction en pause ?
Endde
-
Utilisateur anonyme -
Utilisateur anonyme -
Bonjour,
quelqu'un peut-il m'aider ? J'aimerais créer une fonction "Pause" dans mon programme.
Voilà ce que j'ai déjà écrit :
from tkinter import *
from time import sleep
from random import randint
def demarrer():
global x1, x2, y1, y2
y = 1000
while x1<960:
no = randint(1,2)
if no==1:
x1 += 1
can.coords(BMW, x1, y1)
y = y1
elif no==2:
x2 += 1
can.coords(Volkswagen, x2, y2)
y = y2
sleep(0.001)
can.update()
def reinit():
global x1, x2, y1, y2
x1, y1 = 70, 230
x2, y2 = 70, 370
can.coords(BMW, x1, y1)
x1, y1 = 70, 230
x2, y2 = 70, 370
fen=Tk()
fen.title("Course de voiture")
can = Canvas(fen, width=800, height=600, bg='white') # Surface dans fenetre pour "dessiner"
can.pack(side=TOP, padx=5, pady=5)
b1 = Button(fen, text ='Nouvelle course', width=15, command=reinit)
b1.pack(side=LEFT)
b2 = Button(fen, text ='Partez !', width=15, command=demarrer)
b2.pack(side=LEFT)
b3 = Button(fen, text ='Quitter', width=15, command=fen.quit)
b3.pack(side=RIGHT)
# lignes
can.create_line(150, 0, 150, 700, width=5, fill='green')
can.create_line(0, 150, 1000, 150, width=5, fill="black")
can.create_line(0, 300, 1000, 300, width=5, fill="black")
can.create_line(0, 450, 1000, 450, width=5, fill="black")
can.create_line(750, 0, 750, 700, width=5, fill="red")
can.pack(side=TOP, padx=5, pady=5)
# images
photo1 = PhotoImage(file ='BMW.gif')
BMW = can.create_image(x1, y1, image=photo1)
photo2 = PhotoImage(file ='Volkswagen.gif')
Volkswagen = can.create_image(x2, y2, image=photo2)
fen.mainloop() # Enclenche reception d'evenements
fen.destroy()
quelqu'un peut-il m'aider ? J'aimerais créer une fonction "Pause" dans mon programme.
Voilà ce que j'ai déjà écrit :
from tkinter import *
from time import sleep
from random import randint
def demarrer():
global x1, x2, y1, y2
y = 1000
while x1<960:
no = randint(1,2)
if no==1:
x1 += 1
can.coords(BMW, x1, y1)
y = y1
elif no==2:
x2 += 1
can.coords(Volkswagen, x2, y2)
y = y2
sleep(0.001)
can.update()
def reinit():
global x1, x2, y1, y2
x1, y1 = 70, 230
x2, y2 = 70, 370
can.coords(BMW, x1, y1)
x1, y1 = 70, 230
x2, y2 = 70, 370
fen=Tk()
fen.title("Course de voiture")
can = Canvas(fen, width=800, height=600, bg='white') # Surface dans fenetre pour "dessiner"
can.pack(side=TOP, padx=5, pady=5)
b1 = Button(fen, text ='Nouvelle course', width=15, command=reinit)
b1.pack(side=LEFT)
b2 = Button(fen, text ='Partez !', width=15, command=demarrer)
b2.pack(side=LEFT)
b3 = Button(fen, text ='Quitter', width=15, command=fen.quit)
b3.pack(side=RIGHT)
# lignes
can.create_line(150, 0, 150, 700, width=5, fill='green')
can.create_line(0, 150, 1000, 150, width=5, fill="black")
can.create_line(0, 300, 1000, 300, width=5, fill="black")
can.create_line(0, 450, 1000, 450, width=5, fill="black")
can.create_line(750, 0, 750, 700, width=5, fill="red")
can.pack(side=TOP, padx=5, pady=5)
# images
photo1 = PhotoImage(file ='BMW.gif')
BMW = can.create_image(x1, y1, image=photo1)
photo2 = PhotoImage(file ='Volkswagen.gif')
Volkswagen = can.create_image(x2, y2, image=photo2)
fen.mainloop() # Enclenche reception d'evenements
fen.destroy()
Mon programme fait avancer deux images du côté gauche du canvas jusqu'au côté droit.
J'aimerais qu'on puisse appuyer sur un bouton "pause" afin de faire que les images s'arrête, et de pouvoir appuyer dur un bouton "démarrer", pour qu'elles repartent.
Ca c'est le bouton démarrer:
Je pense que le programme ne marche pas car je ne vous ai pas transmis les images, je vais essayer de le faire.
Merci
Sauriez-vous m'expliquer comment marche ce programme ?