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 :

probleme importation image canevas


Sujet :

Tkinter Python

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2019
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2019
    Messages : 22
    Points : 17
    Points
    17
    Par défaut probleme importation image canevas
    Bonjour, je n'arrive pas à importer une image dans une canevas. merci pour votre aide


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # --------------------------------------------------------------------
    # creation image photo
    # --------------------------------------------------------------------
    image = PIL.Image.open("kard.png")
    photo = PIL.ImageTk.PhotoImage(image)
     
    # --------------------------------------------------------------------
    # creation canvas
    # --------------------------------------------------------------------
    self.canphoto = Canvas(self.fenetre, width = photo.width(), height = photo.height())
    self.canphoto.create_image(40, 60, anchor=NW, image=photo)
    self.canphoto.grid(row=1, column=1, rowspan=2, padx=20, pady=60,sticky = W)

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 283
    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 283
    Points : 36 770
    Points
    36 770
    Par défaut
    Salut,

    Dans ce problème là, le type de la variable "photo" permettra (ou pas) de garder une référence à l'image.
    => montrer des instructions sans leur contexte ne sert à rien.

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

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2019
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2019
    Messages : 22
    Points : 17
    Points
    17
    Par défaut
    merci,
    voila le code entier

    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
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    from tkinter import *
     
    import PIL
    from PIL import Image, ImageTk
    import pickle
     
     
    #--------------------------------------------------------------------
        #reglage
    #--------------------------------------------------------------------
    couleur = (48, 47, 47)
    couleurmes = (113, 113, 113)
     
     
     
     
     
    #--------------------------------------------------------------------
        #creation de la class
    #--------------------------------------------------------------------
    class Appli():
        def __init__(self):
            self.fenetre = Tk()
            self.fenetre.geometry("1020x500")
            self.fenetre.title('Appli')
            self.fenetre.configure(background="gray11")
            self.fenetre.configure(highlightbackground="red")
            self.fenetre.configure(borderwidth="10")
            self.fenetre.configure(relief="ridge")
            self.fenetre.attributes('-alpha', 0.98)
            self.objet()
     
     
     
        #methode pour objets
        def objet(self):
     
            self.listeF = ['NOM :', 'PRENOM :', 'ADRESSE :', 'N° TELE :', 'MAIL :' ]
     
     
     
            R_span = 2
            R_pady = 60
            chemin_fichier = 'kard.png'  # Fichier dans le dossier de ce script
     
     
     
     
            #creation graphique
     
            for x in self.listeF:
     
                x = Label(self.fenetre, text=x, bg='gray11', fg='azure')
                x.grid(row=1, column=2, rowspan =  R_span, padx=30,pady = R_pady, sticky = NW)
                x = Entry(self.fenetre, bg='azure', fg='gray11')
                x.grid(row=1, column=3, rowspan=R_span ,padx=30, pady= R_pady, sticky = N)
     
                #recuperation de la list
     
     
                R_span += 1
                R_pady +=50
     
     
            # Label Massage
     
            self.lab_mes = Label(self.fenetre, text='MESSAGE : ', bg='gray11', fg='azure')
            self.lab_mes.grid(row=1, column=4, padx=60, rowspan = 2, pady=60, sticky = N)
     
            # Massage
            self.MES = Entry(self.fenetre, bg='azure', fg='gray11')
            self.MES.grid(row=2, column=4, padx=20, rowspan=1, ipady=55,ipadx=70, pady=0, sticky=N)
     
            # Bouton
            self.bouton = Button(self.fenetre, text='Valider',fg="azure", command=self.Entree_Get ,
                        activeforeground = 'gray11',highlightbackground="gray11", width= 10, height=2,bd =5)
            self.bouton.grid(row=2, column=4,rowspan = 3)
     
     
            # --------------------------------------------------------------------
            # creation image photo
            # --------------------------------------------------------------------
            image = PIL.Image.open("kard.png")
            photo = PIL.ImageTk.PhotoImage(image)
     
            # --------------------------------------------------------------------
            # creation canvas
            # --------------------------------------------------------------------
            self.canphoto = Canvas(self.fenetre, width = photo.width(), height = photo.height())
            self.canphoto.create_image(40, 60, anchor=NW, image=photo)
            self.canphoto.grid(row=1, column=1, rowspan=2, padx=20, pady=60,sticky = W)
     
     
        #--------------------------------------------------------------------
                            #creation fishier data
        #--------------------------------------------------------------------
            self.list_general = []
     
        def savetexte(self, n):  # enregistrement des données dans un fichier
            fichier = open('data.txt', 'wb')
            pickle.dump(n, fichier)  # sérialisation
            fichier.close()
     
        #def recuptext(self):  # récupération des données
         #   fichier = open('data.txt', 'rb')
          #  M = pickle.load(fichier)  # désérialisation
           # fichier.close()
     
        # --------------------------------------------------------------------
        #methode du boutton valid
        # --------------------------------------------------------------------
     
        def Entree_Get(self):
     
            list_Info = []
            self.list_general.append(list_Info)
            for widget in self.fenetre.winfo_children():
                if widget.winfo_class() == 'Entry':
                    list_Info.append(widget.get())
     
            self.savetexte(self.list_general)
            print(self.list_general)
     
     
     
     
    #variable lié à l'application
    application = Appli()
    application.fenetre.mainloop()

  4. #4
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 283
    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 283
    Points : 36 770
    Points
    36 770
    Par défaut
    Ben vous remplacez "photo" par "self.photo"...

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

  5. #5
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2019
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2019
    Messages : 22
    Points : 17
    Points
    17
    Par défaut
    Merci, ça marche, par contre, l'image n'est pas centré.

    Voici le code:



    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
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    from tkinter import *
     
    import PIL
    from PIL import Image, ImageTk
    import pickle
     
     
    #--------------------------------------------------------------------
        #reglage
    #--------------------------------------------------------------------
    couleur = (48, 47, 47)
    couleurmes = (113, 113, 113)
     
     
     
     
     
    #--------------------------------------------------------------------
        #creation de la class
    #--------------------------------------------------------------------
    class Appli():
        def __init__(self):
            self.fenetre = Tk()
            self.fenetre.geometry("1020x500")
            self.fenetre.title('Appli')
            self.fenetre.configure(background="gray11")
            self.fenetre.configure(highlightbackground="red")
            self.fenetre.configure(borderwidth="10")
            self.fenetre.configure(relief="ridge")
            self.fenetre.attributes('-alpha', 0.98)
            self.objet()
     
     
     
        #methode pour objets
        def objet(self):
     
            self.listeF = ['NOM :', 'PRENOM :', 'ADRESSE :', 'N° TELE :', 'MAIL :' ]
     
     
     
            R_span = 2
            R_pady = 60
            chemin_fichier = 'kard.png'  # Fichier dans le dossier de ce script
     
     
     
     
            #creation graphique
     
            for x in self.listeF:
     
                x = Label(self.fenetre, text=x, bg='gray11', fg='azure')
                x.grid(row=1, column=2, rowspan =  R_span, padx=30,pady = R_pady, sticky = NW)
                x = Entry(self.fenetre, bg='azure', fg='gray11')
                x.grid(row=1, column=3, rowspan=R_span ,padx=30, pady= R_pady, sticky = N)
     
                #recuperation de la list
     
     
                R_span += 1
                R_pady +=50
     
     
            # Label Massage
     
            self.lab_mes = Label(self.fenetre, text='MESSAGE : ', bg='gray11', fg='azure')
            self.lab_mes.grid(row=1, column=4, padx=60, rowspan = 2, pady=60, sticky = N)
     
            # Massage
            self.MES = Entry(self.fenetre, bg='azure', fg='gray11')
            self.MES.grid(row=2, column=4, padx=20, rowspan=1, ipady=55,ipadx=70, pady=0, sticky=N)
     
            # Bouton
            self.bouton = Button(self.fenetre, text='Valider',fg="azure", command=self.Entree_Get ,
                        activeforeground = 'gray11',highlightbackground="gray11", width= 10, height=2,bd =5)
            self.bouton.grid(row=2, column=4,rowspan = 3)
     
     
            # --------------------------------------------------------------------
            # creation image photo
            # --------------------------------------------------------------------
            self.image = PIL.Image.open("kard.png")
            self.photo = PIL.ImageTk.PhotoImage(self.image)
     
            # --------------------------------------------------------------------
            # creation canvas
            # --------------------------------------------------------------------
            self.canphoto = Canvas(self.fenetre, width = self.photo.width(), height = self.photo.height())
            self.canphoto.create_image(40, 60, anchor=NW, image=self.photo)
            self.canphoto.grid(row=1, column=1, rowspan=2, padx=20, pady=60,sticky = W)
     
     
        #--------------------------------------------------------------------
                            #creation fishier data
        #--------------------------------------------------------------------
            self.list_general = []
     
        def savetexte(self, n):  # enregistrement des données dans un fichier
            fichier = open('data.txt', 'wb')
            pickle.dump(n, fichier)  # sérialisation
            fichier.close()
     
        #def recuptext(self):  # récupération des données
         #   fichier = open('data.txt', 'rb')
          #  M = pickle.load(fichier)  # désérialisation
           # fichier.close()
     
        # --------------------------------------------------------------------
        #methode du boutton valid
        # --------------------------------------------------------------------
     
        def Entree_Get(self):
     
            list_Info = []
            self.list_general.append(list_Info)
            for widget in self.fenetre.winfo_children():
                if widget.winfo_class() == 'Entry':
                    list_Info.append(widget.get())
     
            self.savetexte(self.list_general)
            print(self.list_general)
     
     
     
     
    #variable lié à l'application
    application = Appli()
    application.fenetre.mainloop()

  6. #6
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 283
    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 283
    Points : 36 770
    Points
    36 770
    Par défaut
    Citation Envoyé par SamyPyth Voir le message
    Merci, ça marche, par contre, l'image n'est pas centré.
    Vous cherchez à faire quoi en écrivant self.canphoto.create_image(40, 60, anchor=NW, image=photo)? Si ouvrir la documentation du Canvas c'est trop compliqué, sachez que Label(...image=...) fonctionne aussi et est bien plus simple à utiliser.

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

Discussions similaires

  1. [CKEditor] ckeditor probleme affichage image serveur v2 important
    Par kate59 dans le forum Bibliothèques & Frameworks
    Réponses: 1
    Dernier message: 03/04/2016, 19h07
  2. probleme avec image reactive
    Par pit9.76 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 02/11/2005, 21h05
  3. [Upload] Probleme upload images
    Par yveslens dans le forum Langage
    Réponses: 6
    Dernier message: 22/08/2005, 09h42
  4. [Classpath] probleme import de librairies
    Par logica dans le forum Général Java
    Réponses: 17
    Dernier message: 05/08/2005, 12h41
  5. Probleme d'image temporaire
    Par mIch°° dans le forum Composants VCL
    Réponses: 14
    Dernier message: 03/12/2004, 10h05

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