This is an archive copy of wikilivres.ca as of 18 February 2017 (from the WikiTeam dump). The original site is closed.

Page:Swinnen - Apprendre Python, 2010.djvu/130: Difference between revisions

Free texts and images.
Jump to navigation Jump to search
imported>Zephyrus
 
imported>Zephyrus
o
 
(One intermediate revision by the same user not shown)
Page body (to be transcluded):Page body (to be transcluded):
Line 5: Line 5:
[[Image:Python 8 14.png|350px|right|Figure 16]]
[[Image:Python 8 14.png|350px|right|Figure 16]]


<div style="float:center; margin-left:auto; margin-right:auto; padding:0%; font-size:85%; width:550px; background:whitesmoke; color:darkblue;">
<div style="float:center; margin-left:auto; margin-right:auto; padding:0%; font-size:85%; width:750px; background:whitesmoke; color:darkblue;">
{|style="background:whitesmoke; color:darkblue; padding:0%;"
{|style="background:whitesmoke; color:darkblue; padding:0%;"
|width="40%;"|
|width="95%;"|
<poem>
<poem>
{{br0}}from tkinter import *
{{br0}}from tkinter import *
Line 41: Line 41:
     flag =1
     flag =1
     move()
     move()
 
</poem>
#========== Programme principal =============
|}
</div>
<div style="float:center; margin-left:auto; margin-right:auto; padding:0%; font-size:85%; width:750px; background:whitesmoke; color:darkblue;">
{|style="background:whitesmoke; color:darkblue; padding:0%;"
|width="95%;"|
<poem>
{{br0}}
<nowiki>#</nowiki>========== Programme principal =============


# les variables suivantes seront utilisées de manière globale :
# les variables suivantes seront utilisées de manière globale :

Latest revision as of 19:50, 23 April 2012

This page has been proofread


108
Chapitre 8 — Utilisation de fenêtres et de graphismes



Animation automatique - Récursivité

Pour conclure cette première prise de contact avec l’interface graphique tkinter, voici un dernier exemple d’animation, qui fonctionne cette fois de manière autonome dès qu’on l’a mise en marche.

Figure 16
Figure 16


from tkinter import *

  1. définition des gestionnaires
  2. d'événements :


def move():
  "déplacement de la balle"
  global x1, y1, dx, dy, flag
  x1, y1 = x1 +dx, y1 + dy
  if x1 >210:
      x1, dx, dy = 210, 0, 15
  if y1 >210:
      y1, dx, dy = 210, -15, 0
  if x1 <10:
      x1, dx, dy = 10, 0, -15
  if y1 <10:
      y1, dx, dy = 10, 15, 0
  can1.coords(oval1,x1,y1,x1+30,y1+30)
  if flag >0:
      fen1.after(50,move) # => boucler, après 50 millisecondes

def stop_it():
  "arrêt de l'animation"
  global flag
  flag =0

def start_it():
  "démarrage de l'animation"
  global flag
  if flag ==0: # pour ne lancer qu'une seule boucle
     flag =1
     move()



#========== Programme principal =============

  1. les variables suivantes seront utilisées de manière globale :

x1, y1 = 10, 10 # coordonnées initiales
dx, dy = 15, 0 # 'pas' du déplacement
flag =0 # commutateur

  1. Création du widget principal ("parent") :

fen1 = Tk()
fen1.title("Exercice d'animation avec tkinter")

  1. création des widgets "enfants" :

can1 = Canvas(fen1,bg='dark grey',height=250, width=250)
can1.pack(side=LEFT, padx =5, pady =5)
oval1 = can1.create_oval(x1, y1, x1+30, y1+30, width=2, fill='red')
bou1 = Button(fen1,text='Quitter', width =8, command=fen1.quit)
bou1.pack(side=BOTTOM)