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 :

Problème avec mon programme lors du passage avec un Canvas


Sujet :

Tkinter Python

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2018
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2018
    Messages : 7
    Points : 3
    Points
    3
    Par défaut Problème avec mon programme lors du passage avec un Canvas
    Bonjour, je suis un élève de Terminale et je dois réaliser un projet en ISN. Mon projet porte sur un jeu de loto, voici mon programme :

    Liste1=[]
    for m in range (7):
    for n in range (7):
    Liste1.append((50*n,50*m))

    Liste2=[]
    for o in range (49)
    Liste2.append(o)

    for n in range(1):
    Im = Image.open(File)
    Choice = choice(Liste2)
    k = Liste1[Choice][0]
    l = Liste1[Choice][1]
    Get = Im.getpixel(Liste1[Choice])
    #Gris
    if Get==(100,100,100):
    for i in range(50):
    for j in range(50):
    Im.putpixel((k+i,l+j),(75,75,75))
    #Rouge
    elif Get==(255,25,25):
    for i in range(50):
    for j in range(50):
    Im.putpixel((k+i,l+j),(230,0,0))
    #Bleu
    elif Get==(50,100,255):
    for i in range(50):
    for j in range(50):
    Im.putpixel((k+i,l+j),(25,75,230))
    #Jaune
    else:
    for i in range(50):
    for j in range(50):
    Im.putpixel((k+i,l+j),(200,200,0))
    Im.show()

    Cependant quand j'essaie d'insérer le tout dans un Canvas et d'affecter ce programme à un bouton, le programme ne marche plus et je reçois comme erreur :

    Im = Image.open(File)
    AttributeError: 'PhotoImage' object has no attribute 'open'

    Sauriez-vous m'éclairez s'il vous plait ? Merci

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 287
    Points : 36 778
    Points
    36 778
    Par défaut
    Salut,

    Citation Envoyé par Emile315 Voir le message
    Cependant quand j'essaie d'insérer le tout dans un Canvas et d'affecter ce programme à un bouton, le programme ne marche plus et je reçois comme erreur :

    Im = Image.open(File)
    AttributeError: 'PhotoImage' object has no attribute 'open'

    Sauriez-vous m'éclairez s'il vous plait ?
    Votre programme est incomplet puisque vous avez omis les "import".... Mais de fait, l'objet Image de Pillow/PIL n'est pas le même que celui qui vient avec tkinter.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2018
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2018
    Messages : 7
    Points : 3
    Points
    3
    Par défaut
    Merci de ta réponse, alors oui je sais assez bien comment fonctionne tkinter, je sais que ce n'est pas la fonction image.show() etc .. Par contre je n'étais pas au courant de import, donc je verrai de mon côté, en tout cas encore merci

  4. #4
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2018
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2018
    Messages : 7
    Points : 3
    Points
    3
    Par défaut
    Enfin j'ai dû mal comprendre, j'ai bien import tkinter ainsi que PIL donc pas de soucis niveau là.

  5. #5
    Membre chevronné
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 609
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 609
    Points : 2 073
    Points
    2 073
    Par défaut
    Citation Envoyé par Emile315 Voir le message
    Enfin j'ai dû mal comprendre, j'ai bien import tkinter ainsi que PIL donc pas de soucis niveau là.
    Cela ne se voit pas.
    Remarque : poste ton code entre balises (#).
    Pas d'aide par mp.

  6. #6
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2018
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2018
    Messages : 7
    Points : 3
    Points
    3
    Par défaut
    Voici mon programme en entier si cela ne vous dérange pas :

    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    from tkinter import *
    from random import *
    from PIL import Image
     
    #Importation de l'image
    File = "Image.png"
    Im = Image.open(File)
     
    #Convertion de l'image en .gif
    Im.save("Im.gif")
     
    #Création des carrés de 50x50 pixels
    Liste1 = []
    for m in range(7):
    <div style="margin-left:40px">for n in range(7):</div><div style="margin-left:40px"><div style="margin-left:40px">Liste1.append((50*n, 50*m))</div></div>
    #Création des 49 carrés de 50x50 pixels
    Liste2 = []
    for o in range(49):
    <div style="margin-left:40px">Liste2.append(o)</div>
    #Création d'une fenêtre
    Fen = Tk()
    Fen.title("LOTO Game")
    Fen.geometry("500x545")
    Fen.configure(bg="black")
     
    #Création d'un label "Veuillez choisir une couleur"
    Lab1 = Label(Fen, text="Veuillez choisir une couleur :", width=24, height=2, bg="white")
    Lab1.place(x=160, y=20)
     
    #Création d'une entry "Gagné/Perdu"
    Saisir = StringVar()
    Saisie = Entry(Fen, textvariable=Saisir, width=10)
    Saisie.place(x=50, y=500)
     
    #Création des couleurs
    Gris = "#%02x%02x%02x" % (100, 100, 100)
    Rouge = "#%02x%02x%02x" % (255, 25, 25)
    Bleu = "#%02x%02x%02x" % (50, 100, 255)
    Jaune = "#%02x%02x%02x" % (225, 225, 25)
     
    #Bouton gris
    def numero1():
    <div style="margin-left:40px">global Test</div><div style="margin-left:40px">for n in range(1):</div><div style="margin-left:40px">Choice = choice(Liste2)</div><div style="margin-left:40px">k = Liste1[Choice][0]</div><div style="margin-left:40px">l = Liste1[Choice][1]</div><div style="margin-left:40px">Get = Im.getpixel(Liste1[Choice])</div><div style="margin-left:40px">#Gris</div><div style="margin-left:40px">if Get==(100,100,100):</div><div style="margin-left:40px"><div style="margin-left:40px">for i in range(50):</div></div><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px">for j in range(50):</div></div></div><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px">Im.putpixel((k+i,l+j),(75,75,75))</div></div></div></div><div style="margin-left:40px">#Rouge</div><div style="margin-left:40px">elif Get==(255,25,25):</div><div style="margin-left:40px"><div style="margin-left:40px">for i in range(50):</div></div><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px">for j in range(50):</div></div></div><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px">Im.putpixel((k+i,l+j),(230,0,0))</div></div></div></div><div style="margin-left:40px">#Bleu</div><div style="margin-left:40px">elif Get==(50,100,255):</div><div style="margin-left:40px"><div style="margin-left:40px">for i in range(50):</div></div><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px">for j in range(50):</div></div></div><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px">Im.putpixel((k+i,l+j),(25,75,230))</div></div></div></div><div style="margin-left:40px">#Jaune</div><div style="margin-left:40px">else:</div><div style="margin-left:40px"><div style="margin-left:40px">for i in range(50):</div></div><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px">for j in range(50):</div></div></div><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px"><div style="margin-left:40px">Im.putpixel((k+i,l+j),(200,200,0))</div></div></div></div>
    <div style="margin-left:40px">Im.show()</div>
    #Bouton rouge
    def numero2():
    <div style="margin-left:40px">global Test</div><div style="margin-left:40px">Canv2 = Canvas(Fen, height=348, width=348, bg=Rouge)</div><div style="margin-left:40px">Canv2.place(x=75, y=75)</div>
    #Bouton bleu
    def numero3():
    <div style="margin-left:40px">global Test</div><div style="margin-left:40px">Canv3 = Canvas(Fen, height=348, width=348, bg=Bleu)</div><div style="margin-left:40px">Canv3.place(x=75, y=75)</div>
    #Bouton jaune
    def numero4():
    <div style="margin-left:40px">global Test</div><div style="margin-left:40px">Canv4 = Canvas(Fen, height=348, width=348, bg=Jaune)</div><div style="margin-left:40px">Canv4.place(x=75, y=75)</div>
    #Bouton rejouer
    def numero5():
    <div style="margin-left:40px">global Test</div><div style="margin-left:40px">Canvr = Canvas(Fen, height=348, width=348, bg="Orange")</div><div style="margin-left:40px">Canvr.place(x=75, y=75)</div>
    #Création du bouton gris
    Bou1 = Button(Fen, text="          ", command=numero1, bg=Gris)
    Bou1.place(x=50, y=450)
     
    #Création du bouton rouge
    Bou2 = Button(Fen, text="          ", command=numero2, bg=Rouge)
    Bou2.place(x=135, y=450)
     
    #Création du bouton bleu
    Bou3 = Button(Fen, text="          ", command=numero3, bg=Bleu)
    Bou3.place(x=220, y=450)
     
    #Création du bouton jaune
    Bou4 = Button(Fen, text="          ", command=numero4, bg=Jaune)
    Bou4.place(x=305, y=450)
     
    #Création du bouton quitter
    Bouq = Button(Fen, text=" Quitter ", command=Fen.destroy)
    Bouq.place(x=400, y=450)
     
    #Création du bouton rejouer
    Bour = Button(Fen, text=" Rejouer ", command=numero5, bg="white")
    Bour.place(x=400, y=500)
     
    Canv = Canvas(Fen, height=348, width=348)
    Canv.place(x=75, y=75)
    Image = PhotoImage(file="Im.gif")
    Canv.create_image(175, 175, image=Image)
     
    Fen.mainloop()
    Sachant que les boutons 2, 3 et 4 (l'équivalent Rouge, Bleu et Jaune) auront la même fonctionnalité que le bouton 1 (Gris), le bouton Restart je le ferai plus tard, ça sera l'option d'un autre post sur le forum Et pour l'entry je verrai avec mon prof.
    Retournons au bouton 1, lorsque je lance le programme et que je clique sur le bouton 1 (Gris), ça me lance une visionneuse d'image qui me montre l'image comme je le veux, sauf que ce que je veux vraiment c'est l'afficher dans un canevas et je n'y arrive pas, une idée ? Sachant que lorsque je remplace :

    par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Canv1 = Canvas(Fen, height=348, width=348)
    Canv1.place(x=75, y=75)
    #Image = PhotoImage(file="Im.gif") ou encore #Image = PhotoImage(Im)
    Canv1.create_image(175, 175, image=Im)
    Ou même en rajoutant ce qu'il y a entre (#), ça m'affiche une erreur qui est :

    Exception in Tkinter callback
    Traceback (most recent call last):
    File "D:\Emile\Program Files\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
    File "C:/Users/emile/OneDrive/Documents/Cours/Term S/Informatique et Sciences du Numérique/Projet/Projet ISN.py", line 75, in numero1
    Canv1.create_image(175, 175, image=Im)
    File "D:\Emile\Program Files\Anaconda3\lib\tkinter\__init__.py", line 2483, in create_image
    return self._create('image', args, kw)
    File "D:\Emile\Program Files\Anaconda3\lib\tkinter\__init__.py", line 2474, in _create
    *(args + self._options(cnf, kw))))
    _tkinter.TclError: image "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=350x350 at 0x235E0144668>" doesn't exist

    Voilà voilà, un grand merci d'avance pour celui ou celle qui saura m'aider !

    Voilà qui est plus clair

  7. #7
    Membre chevronné
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 609
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 609
    Points : 2 073
    Points
    2 073
    Par défaut
    Relis mon post, s'il te plaît.
    Pas d'aide par mp.

  8. #8
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2018
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2018
    Messages : 7
    Points : 3
    Points
    3
    Par défaut
    Je n'ai pas compris quoi posté entre (#),

  9. #9
    Membre chevronné
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 609
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 609
    Points : 2 073
    Points
    2 073
    Par défaut
    Ton programme copié-collié.
    P.S. Tu peux éditer et modifier tes anciens messages, cela évitera d'empiler des messages inutiles.
    Pas d'aide par mp.

  10. #10
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 287
    Points : 36 778
    Points
    36 778
    Par défaut
    Salut,

    Ce qui est clair c'est que vous essayez un peu tout et n'importe quoi en espérant que çà tombe en marche!
    En écrivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    from PIL import Image
    from tkinter import *
    La première variable Image associée à la class Image de PIL se voit remplacée par celle qui vient avec tkinter. Du coup, çà plante avec:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Im = Image.open(File)
    AttributeError: 'PhotoImage' object has no attribute 'open'
    qui était le sujet de votre premier "post".

    Pour le reste, la question a se poser est ok, tkinter ne supporte que certains format d'image, comment faire pour les autres? Et regarder dans la FAQ la réponse à cette question.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

Discussions similaires

  1. Problèmes avec mon programme
    Par anthony.flet dans le forum Général Python
    Réponses: 7
    Dernier message: 05/09/2012, 08h58
  2. Réponses: 11
    Dernier message: 04/03/2012, 19h12
  3. [Débutant] Problème avec mon programme ( Type de valeur )
    Par Jcvag44800 dans le forum VB.NET
    Réponses: 10
    Dernier message: 21/12/2011, 21h02
  4. [XL-2003] Problème de tirage aléatoire avec mon programme
    Par MARGAR dans le forum Macros et VBA Excel
    Réponses: 12
    Dernier message: 15/11/2011, 15h19
  5. Problème avec mon programme racine
    Par Chicard dans le forum Débuter avec Java
    Réponses: 8
    Dernier message: 25/11/2010, 10h25

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