IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Tkinter Python Discussion :

comment changer de couleur?


Sujet :

Tkinter Python

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    144
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 144
    Points : 68
    Points
    68
    Par défaut comment changer de couleur?
    Bonjour,
    voila : j'ai un petit programme (qui fonctionne )
    j'ai rajouté un changecolour pour que la boule change de couleur à chaque "virage" mais je ne sais pas ou le mettre?
    D'abord est ce juste de creer cette def?
    Merci pour vos réponses
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
     
    #!/usr/bin/env python
    #coding=utf-8
    from Tkinter import *
    # définition des gestionnaires
    # d'événements :
    def move():
     
        global x1, y1, dx, dy, flag,coul
        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 milliseconde
    def stop_it():
     
        global flag
        flag =0
    def start_it():
     
        global flag
        if flag ==0: # pour ne lancer qu'une seule boucle
            flag = 1
            move()
     
    def changecolour():
        if x1>210:
            fill= 'red'
        elif y1>210:
            fill = 'blue'
        elif x1<10:
            fill = 'yellow'
        elif y1<10:
            fill ='black'
     
    #========== Programme principal =============
    # 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
    # Création du widget principal ("parent") :
    fen1 = Tk()
    fen1.title("Exercice d'animation avec Tkinter")
    # 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)
     
     
    bou1 = Button(fen1,text='Quitter', width =8, command=fen1.quit)
    bou1.pack(side=BOTTOM)
    bou2 = Button(fen1, text='Démarrer', width =8, command=start_it)
    bou2.pack()
    bou3 = Button(fen1, text='Arrêter', width =8, command=stop_it)
    bou3.pack()
    # démarrage du réceptionnaire d'évènements (boucle principale) :
    fen1.mainloop()

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    144
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 144
    Points : 68
    Points
    68
    Par défaut
    petite précision :le code roule mais la "balle "est vide " forcement puisque dans le create_oval il n'y a pas de ,fill= 'red' par exemple

    .je voudrais donc savoir ou placer le def ?
    ou s'il y a une meilleure facon de changer de couleur à chaque "virage"?
    merci pour vos réponses

  3. #3
    Expert confirmé Avatar de PauseKawa
    Homme Profil pro
    Technicien Help Desk, maintenance, réseau, système et +
    Inscrit en
    Juin 2006
    Messages
    2 725
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Technicien Help Desk, maintenance, réseau, système et +
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 725
    Points : 4 005
    Points
    4 005
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    def move():
     
        global x1, y1, dx, dy, flag,coul
        x1, y1 = x1 +dx, y1 + dy
        if x1 >210:
            can1.itemconfigure(oval1, fill= 'red')
            x1, dx, dy = 210, 0, 15
        if y1 >210:
            can1.itemconfigure(oval1, fill= 'blue')
            y1, dx, dy = 210, -15, 0
        if x1 <10:
            can1.itemconfigure(oval1, fill= 'yellow')
            x1, dx, dy = 10, 0, -15
        if y1 <10:
            can1.itemconfigure(oval1, fill= 'black')
            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 milliseconde
    @+

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    144
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 144
    Points : 68
    Points
    68
    Par défaut
    parfait cela fonctionne sauf qu'au départ la" boule"est vide et ne se remplit qu'au premier déplacement.
    comme cela :
    Bingo!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
     
    #!/usr/bin/env python
    #coding=utf-8
    from Tkinter import *
    # définition des gestionnaires
    # d'événements :
    def move():
     
        global x1, y1, dx, dy, flag,coul
        x1, y1 = x1 +dx, y1 + dy
        if x1 >210:
            can1.itemconfigure(oval1, fill= 'black')
            x1, dx, dy = 210, 0, 15
        if y1 >210:
            can1.itemconfigure(oval1, fill= 'blue')
            y1, dx, dy = 210, -15, 0
        if x1 <10:
            can1.itemconfigure(oval1, fill= 'yellow')
            x1, dx, dy = 10, 0, -15
        if y1 <10:
            can1.itemconfigure(oval1, fill= 'red')
            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 milliseconde
     
    def stop_it():
     
        global flag
        flag =0
    def start_it():
     
        global flag
        if flag ==0: # pour ne lancer qu'une seule boucle
            flag = 1
            move()
     
     
     
    #========== Programme principal =============
    # 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
    # Création du widget principal ("parent") :
    fen1 = Tk()
    fen1.title("Exercice d'animation avec Tkinter")
    # 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)
    bou2 = Button(fen1, text='Démarrer', width =8, command=start_it)
    bou2.pack()
    bou3 = Button(fen1, text='Arrêter', width =8, command=stop_it)
    bou3.pack()
    # démarrage du réceptionnaire d'évènements (boucle principale) :
    fen1.mainloop()
    On ne peut donc pas appliquer un def changecolour?
    merci PauseKawa

  5. #5
    Expert confirmé Avatar de PauseKawa
    Homme Profil pro
    Technicien Help Desk, maintenance, réseau, système et +
    Inscrit en
    Juin 2006
    Messages
    2 725
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Technicien Help Desk, maintenance, réseau, système et +
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 725
    Points : 4 005
    Points
    4 005
    Par défaut
    Bonsoir,

    Si bien sur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    #!/usr/bin/env python
    #coding=utf-8
    from Tkinter import *
    # définition des gestionnaires
    # d'événements :
     
    def move():
        global x1, y1, dx, dy, flag,coul
        x1, y1 = x1 +dx, y1 + dy
        if x1 >210:
            changecolour()
            x1, dx, dy = 210, 0, 15
        elif y1 >210:
            changecolour()
            y1, dx, dy = 210, -15, 0
        elif x1 <10:
            changecolour()
            x1, dx, dy = 10, 0, -15
        elif y1 <10:
            changecolour()
            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 milliseconde
     
    def stop_it():
        global flag
        flag =0
     
    def start_it():
        global flag
        if flag ==0: # pour ne lancer qu'une seule boucle
            flag = 1
            move()
     
    def changecolour():
        if x1>210:
            can1.itemconfigure(oval1, fill= 'red')
        elif y1>210:
            can1.itemconfigure(oval1, fill= 'blue')
        elif x1<10:
            can1.itemconfigure(oval1, fill= 'yellow')
        elif y1<10:
            can1.itemconfigure(oval1, fill= 'black')
     
     
    #========== Programme principal =============
    # 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
    # Création du widget principal ("parent") :
    fen1 = Tk()
    fen1.title("Exercice d'animation avec Tkinter")
    # 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)
    bou2 = Button(fen1, text='Démarrer', width =8, command=start_it)
    bou2.pack()
    bou3 = Button(fen1, text='Arrêter', width =8, command=stop_it)
    bou3.pack()
    # démarrage du réceptionnaire d'évènements (boucle principale) :
    fen1.mainloop()
    Mais tu reprend le code de move() et le duplique inutilement.

    @+

    Ps: Je trouve qu'il y a pas mal de global dans ton code.

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    144
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 144
    Points : 68
    Points
    68
    Par défaut
    Bonjour,

    Ps: Je trouve qu'il y a pas mal de global dans ton code.
    tu veux dire que je devrais l'encapsuler dans une classe?

  7. #7
    Expert confirmé Avatar de PauseKawa
    Homme Profil pro
    Technicien Help Desk, maintenance, réseau, système et +
    Inscrit en
    Juin 2006
    Messages
    2 725
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Technicien Help Desk, maintenance, réseau, système et +
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 725
    Points : 4 005
    Points
    4 005
    Par défaut
    Bonjour,

    C'étais juste un avis perso de débutant.
    Je n'aime pas les global, je trouve que c'est source à erreur.
    Dans le cadre de ton code c'est sans conséquence et cela complique inutilement bien sur.

    Ceci dit avec ton idée de class cela donne quelque chose comme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    #!/usr/bin/env python
    #coding=utf-8
    from Tkinter import *
     
    class Mes_Variables():
        flag = None
        x = None
        y = None
        dx = None
        dy = None
     
    # définition des gestionnaires
    # d'événements :
    def move():
        Mes_Variables.x1, Mes_Variables.y1 = Mes_Variables.x1 + Mes_Variables.dx, Mes_Variables.y1 + Mes_Variables.dy
        if Mes_Variables.x1 >210:
            can1.itemconfigure(oval1, fill= 'black')
            Mes_Variables.x1, Mes_Variables.dx, Mes_Variables.dy = 210, 0, 15
        elif Mes_Variables.y1 >210:
            can1.itemconfigure(oval1, fill= 'blue')
            Mes_Variables.y1, Mes_Variables.dx, Mes_Variables.dy = 210, -15, 0
        elif Mes_Variables.x1 <10:
            can1.itemconfigure(oval1, fill= 'yellow')
            Mes_Variables.x1, Mes_Variables.dx, Mes_Variables.dy = 10, 0, -15
        elif Mes_Variables.y1 <10:
            can1.itemconfigure(oval1, fill= 'red')
            Mes_Variables.y1, Mes_Variables.dx, Mes_Variables.dy = 10, 15, 0
        can1.coords(oval1, Mes_Variables.x1, Mes_Variables.y1, Mes_Variables.x1+30, Mes_Variables.y1+30)
        if Mes_Variables.flag == 1:
            fen1.after(50, move) # => boucler après 50 milliseconde
     
    def stop_it():
        Mes_Variables.flag = 0
     
    def start_it():
        if Mes_Variables.flag == 0: # pour ne lancer qu'une seule boucle
            Mes_Variables.flag = 1
            move()
     
     
    #========== Programme principal =============
    # les variables suivantes seront utilisées de manière globale :
    Mes_Variables.x1, Mes_Variables.y1 = 10, 10 # coordonnées initiales
    Mes_Variables.dx, Mes_Variables.dy = 15, 0 # 'pas' du déplacement
    Mes_Variables.flag = 0 # commutateur
    # Création du widget principal ("parent") :
    fen1 = Tk()
    fen1.title("Exercice d'animation avec Tkinter")
    # 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(Mes_Variables.x1, Mes_Variables.y1, Mes_Variables.x1+30, Mes_Variables.y1+30, width=2, fill='red')
     
     
    bou1 = Button(fen1,text='Quitter', width=8, command=fen1.quit)
    bou1.pack(side=BOTTOM)
    bou2 = Button(fen1, text='Démarrer', width=8, command=start_it)
    bou2.pack()
    bou3 = Button(fen1, text='Arrêter', width=8, command=stop_it)
    bou3.pack()
    # démarrage du réceptionnaire d'évènements (boucle principale) :
    fen1.mainloop()
    Juste pour faire bouger un oval garde tes global

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    144
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 144
    Points : 68
    Points
    68
    Par défaut
    Bonjour,
    je voudrais bien etre un débutant de ton niveau...
    Merci pour la classe.
    Bonne journée

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 26/07/2005, 10h45
  2. Réponses: 7
    Dernier message: 28/06/2005, 11h53
  3. [phpBB] Comment changer les couleurs
    Par ludolecho dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 2
    Dernier message: 19/05/2005, 08h20
  4. comment changer la couleur du crayon?
    Par meli0207 dans le forum MFC
    Réponses: 10
    Dernier message: 07/05/2005, 10h41
  5. Réponses: 2
    Dernier message: 01/12/2004, 22h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo