Partage
  • Partager sur Facebook
  • Partager sur Twitter

Changer couleur Canvas en fonction de la ligne csv

Sujet résolu
    2 mars 2021 à 17:01:30

    Voici mon code:

    from tkinter import *
    import tkinter.font as font
    import csv 
    fichier = open("tableau_periodique.csv", "r")
    lecteur = csv.DictReader(fichier,delimiter=',')
    #Création des fonctions
    def fen_info():
        fenetre_info = Canvas(fen, width=500, height=257, bg = "white", highlightthickness=5,highlightbackground=atome['bg'])
        fenetre_info.place(x=600, y = 0)
        taille=font.Font(size=15)
        bouton1=Button(fen,text="-Informations sur l'atome-",width=45,height=5,font=taille)
        bouton1['font'] = taille
        bouton1.place(x=603,y=3)
        bouton2=Button(fen,text="-Ajouter à la création d'une molécule-",width=45,height=5,font=taille)
        bouton2['font'] = taille
        bouton2.place(x=603,y=134)
    
    #création fenetre
    fen = Tk()
    fen.title("Teableau périodique des éléments")
    fen.geometry("1260x720")
    fen.minsize(1260,720)
    fen.maxsize(1260,720)
    fen.iconbitmap("molecule.ico")
    fen.config(bg = "white")
    
    #création du tableau
    width = 3
    height = 2
    xx = 0
    yy = 40
    hydrogene = Button(fen, text="H",width=width, height=height, bg = "grey",command=fen_info)
    hydrogene.place(x=0,y=0)
    
    helium = Button(fen, width=width, height=height, bg = "cyan", text="He",command=fen_info)
    helium.place(x=510,y=0)
    
    for ligne in lecteur:
        
        if ligne["famille"] == "Gaz rares":
                atome = Button(fen, width=width, height=height, bg = "cyan", text=ligne["symbole"],command=fen_info)
                atome.place(x=xx,y=yy)
                xx += 30
        
        elif ligne["famille"] == "Métaux alcalins":
            atome = Button(fen, text=ligne["symbole"],width=width, height=height, bg = "#FF5733",command=fen_info)
            atome.place(x=xx,y=yy)
            xx += 30
        
        elif ligne["famille"] == "Métaux alcalino-ferreux":
            atome = Button(fen,text=ligne["symbole"], width=width, height=height, bg = "yellow",command=fen_info)
            atome.place(x=xx,y=yy)
            if ligne["symbole"] == "Mg" or ligne["symbole"] == "Be":
                xx += 330
            else:
                xx += 30
        
        elif ligne["famille"] == "Non métaux":
            atome = Button(fen, width=width, height=height, bg = "green",text=ligne["symbole"],command=fen_info)
            atome.place(x=xx,y=yy)
            xx += 30
        
        elif ligne["famille"] == "Métaux de transition":
            if ligne["symbole"] == "Hf" or ligne["symbole"] == "Rf":
                xx -= 420
            atome = Button(fen, width=width, height=height, bg = "#FF46A5",text=ligne["symbole"],command=fen_info)
            atome.place(x=xx,y=yy)
            xx += 30
    
        elif ligne["famille"] == "Lanthanides":
            if ligne["symbole"] == 'La':
                for i in range(2):
                    asterix = Button(fen, width=width, height=height, bg = "#FF9146",text="*")
                    asterix.place(x=xx,y=yy)
                    yy += 100
                yy -= 200
            yy += 100
            xx += 30
            atome = Button(fen, width=width, height=height, bg = "#FF9146", text=ligne["symbole"],command=fen_info)
            atome.place(x=xx,y=yy)
            yy -= 100
        
        elif ligne["famille"] == "Actinides":
            if ligne["symbole"] == 'Ac':
                for i in range(2):
                    asterix2 = Button(fen, width=width, height=height, bg = "#AE46FF",text="**")
    
                    asterix2.place(x=xx,y=yy)
                    yy += 100
                yy -= 200
            yy += 100
            xx += 30
            atome = Button(fen, width=width, height=height, bg = "#AE46FF",text=ligne["symbole"],command=fen_info)
            atome.place(x=xx,y=yy)
            yy -= 100
        
        elif ligne["famille"] == "Métaux pauvres":
            atome = Button(fen, width=width, height=height, bg = "blue",text=ligne["symbole"],command=fen_info)
            atome.place(x=xx,y=yy)
            xx += 30    
        
        if xx >= 540:
            yy += 40
            xx = 0
        
    fen.mainloop()
    fichier.close()

    Je sais qu'il est tres long, voir trop mais ce n'est pas le sujet.

    Pour la fonction fen_info, je souhaiterai créer une boucle qui permet de changer la couleur de highlightthickness en fonction de la couleur définie pour chaque atome.

    J'espere que c'est clair sinon voici une photo de ce qu'il doit se passer.

    Merci d'avance :)

    -
    Edité par ProbbyistProbbyist 2 mars 2021 à 17:04:24

    • Partager sur Facebook
    • Partager sur Twitter
      2 mars 2021 à 17:28:48

      Bonjour.

      Personnellement, je passerai par un dictionnaire "Couleurs_Famille_Atomes" et, lors de l'appel de la fonction, il faut passer en paramètre la valeur concernée et modifier la fonction "fen_info" en conséquence.

      >>> Couleurs_Famille_Atomes = {
          "Gaz rares": "cyan",
          "Métaux alcalins": "#FF5733",
          "Métaux alcalino-ferreux": "yellow",
          "Non métaux": "green",
          "Métaux de transition": "#FF46A5",
          "Lanthanides": "#FF9146",
          "Actinides": "#AE46FF",
          "Métaux pauvres": "blue"}
      
      >>> Couleurs_Famille_Atomes["Non métaux"]
      'green'
      

      Edit : Tu peux d'ailleurs utiliser ce même dictionnaire pour les cases de ton tableau. Autant le rentabiliser.

      -
      Edité par PB68 2 mars 2021 à 17:52:55

      • Partager sur Facebook
      • Partager sur Twitter

      PB68

      Anonyme
        2 mars 2021 à 17:52:04

        Slt,

        Tu peux également récupérer la couleur par la méthode cget()

        color = button.cget("bg")
        # ou
        color = button["bg"]



        • Partager sur Facebook
        • Partager sur Twitter
          2 mars 2021 à 18:35:57

          Merci beaucoup pour vos réponses.

          Mais ne pourrais-je pas créer une boucle au lieux de REmettre pleins de if et elif ?

          • Partager sur Facebook
          • Partager sur Twitter
          Anonyme
            2 mars 2021 à 19:01:04

            Pas besoin de boucle, tu peux passer par une fonction lambda :

            from tkinter import *
            import tkinter.font as font
            
            def fen_info(col=None):
                print(col)
             
            #création fenetre
            fen = Tk()
            fen.title("Teableau périodique des éléments")
            fen.geometry("400x300")
            fen.config(bg = "white")
            atome = Button(fen, text="symbole",bg = "#FF5733",command=lambda: fen_info("#FF5733"))
            atome.pack()
            fen.mainloop()



            -
            Edité par Anonyme 2 mars 2021 à 19:02:27

            • Partager sur Facebook
            • Partager sur Twitter
              2 mars 2021 à 19:18:40

              Diablo76 a écrit:

              Pas besoin de boucle, tu peux passer par une fonction lambda :

              from tkinter import *
              import tkinter.font as font
              
              def fen_info(col=None):
                  print(col)
               
              #création fenetre
              fen = Tk()
              fen.title("Teableau périodique des éléments")
              fen.geometry("400x300")
              fen.config(bg = "white")
              atome = Button(fen, text="symbole",bg = "#FF5733",command=lambda: fen_info("#FF5733"))
              atome.pack()
              fen.mainloop()



              -
              Edité par Diablo76 il y a 15 minutes

              Merci beaucoup, tu m'as été d'une grande aide !

              • Partager sur Facebook
              • Partager sur Twitter
              Anonyme
                2 mars 2021 à 20:02:47

                En espérant que tu as compris, Python apporte beaucoup plus que des boucles ou des conditions 'if elif else"  ;)

                -
                Edité par Anonyme 2 mars 2021 à 20:03:34

                • Partager sur Facebook
                • Partager sur Twitter

                Changer couleur Canvas en fonction de la ligne csv

                × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                • Editeur
                • Markdown