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

Python Discussion :

insertion de plusieurs photos dans un canevas tkinter


Sujet :

Python

  1. #1
    Membre averti
    Homme Profil pro
    ingé automaticien
    Inscrit en
    Juillet 2011
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : ingé automaticien
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2011
    Messages : 37
    Par défaut insertion de plusieurs photos dans un canevas tkinter
    bonjour à tous,
    je tente depuis un bon moment d'utiliser les canevas de tkinter et je rencontre un petit probleme.
    en gros je souhaite poser plusieurs images dans un popup tkinter.

    dans un premier temps j'ai tenté de faire plusieurs canevas indépendant et j'ai fait avec le bout code suivant:

    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
    can1 = Canvas(fen1, width =80, height =80, bg ='red')
    photo = PhotoImage(file ='image\img jackpot\poire.gif')
    item = can1.create_image(40, 40, image =photo)
     
    can2 = Canvas(fen1, width =80, height =80, bg ='red')
    photo = PhotoImage(file ='image\img jackpot\poire.gif')
    item = can2.create_image(40, 40, image =photo)
     
    can3 = Canvas(fen1, width =80, height =80, bg ='red')
    photo = PhotoImage(file ='image\img jackpot\poire.gif')
    item = can3.create_image(40, 40, image =photo)
     
    can1.grid(row =4, column =1, columnspan =1)
    can2.grid(row =4, column =2, columnspan =1)
    can3.grid(row =4, column =3, columnspan =1)
    le code complet est le suivant:
    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
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
     
     
    def alea():
    	import random
    	r = random.randint(1,6)
    	return r
     
    def info(x):
            if x==1 :
                    te="banane"
            if x==2 :
                    te="pomme"
            if x==3 :
                    te="poire"
            if x==4 :
                    te="BAR"
            if x==5 :
                    te="cerise"
            if x==6 :
                    te="ananas"        
            return te
     
    def gain(un,de,tr,gain,somme):
            if un==1 and de==1 and tr==1:
                    mul=2
            elif un==1 and de==1:
                    mul=1.1
            elif un==1 and tr==1:
                    mul=1.1
            elif de==1 and tr==1:
                    mul=1.1
     
            elif un==2 and de==2 and tr==2:
                    mul=3
            elif un==2 and de==2:
                    mul=1.2
            elif un==2 and tr==2:
                    mul=1.2
            elif de==2 and tr==2:
                    mul=1.2
     
            elif un==3 and de==3 and tr==3:
                    mul=4
            elif un==3 and de==3:
                    mul=1.3
            elif un==3 and tr==3:
                    mul=1.3
            elif de==3 and tr==3:
                    mul=1.3
     
            elif un==4 and de==4 and tr==4:
                    mul=5
            elif un==4 and de==4:
                    mul=1.4
            elif un==4 and tr==4:
                    mul=1.4
            elif de==4 and tr==4:
                    mul=1.4
     
            elif un==5 and de==5 and tr==5:
                    mul=6
            elif un==5 and de==5:
                    mul=1.5
            elif un==5 and tr==5:
                    mul=1.5
            elif de==5 and tr==5:
                    mul=1.5
     
            elif un==6 and de==6 and tr==6:
                    mul=7
            elif un==6 and de==6:
                    mul=1.6
            elif un==6 and tr==6:
                    mul=1.6
            elif de==6 and tr==6:
                    mul=1.6           
            else :
                    mul=-1
     
            somme=int(somme+gain*mul)
     
            return somme        
     
    def start_it():
     
            "démarrage de l'animation"
            global argent
            global reste
            pari =int(entr1.get())
     
            a=alea()
            b=alea()
            c=alea()
     
            d=info(int(a))
            e=info(int(b))
            f=info(int(c))
     
            txt4.configure(text = d)
            txt5.configure(text = e)
            txt6.configure(text = f)
     
            argent = gain(a,b,c,pari,argent)
     
            txt7.configure(text = 'il vous argent '+str(argent)+' euros')
     
     
     
    import os
    from tkinter import *
     
    argent = 100
     
     
    pari = 0
    e=d=f="vide"
     
    toto = 'il vous argent ',argent,' euros'
    fen1 = Tk()
    # création de widgets 'Label' et 'Entry' :
    txt1 = Label(fen1, text ='bienvenue, vous avez')
    txt2 = Label(fen1, text =argent)
    txt3 = Label(fen1, text ='euro pour jouer')
    txt4 = Label(fen1, text =e)
    txt5 = Label(fen1, text =d)
    txt6 = Label(fen1, text =f)
    txt7 = Label(fen1, text =toto)
    entr1 = Entry(fen1)
     
    #poisitionnement photo
    can1 = Canvas(fen1, width =80, height =80, bg ='red')
    photo = PhotoImage(file ='image\img jackpot\poire.gif')
    item = can1.create_image(40, 40, image =photo)
     
    can2 = Canvas(fen1, width =80, height =80, bg ='red')
    photo = PhotoImage(file ='image\img jackpot\poire.gif')
    item = can2.create_image(40, 40, image =photo)
     
    can3 = Canvas(fen1, width =80, height =80, bg ='red')
    photo = PhotoImage(file ='image\img jackpot\poire.gif')
    item = can3.create_image(40, 40, image =photo)
     
     
     
    bou2 = Button(fen1, text='Démarrer', width =8, command=start_it)
    bou2.pack()
     
     
    txt1.grid(row =1, column =1)
    txt2.grid(row =1, column =2)
    txt3.grid(row =1, column =3)
     
    txt4.grid(row =3, column =1)
    txt5.grid(row =3, column =2)
    txt6.grid(row =3, column =3)
     
    can1.grid(row =4, column =1, columnspan =1)
    can2.grid(row =4, column =2, columnspan =1)
    can3.grid(row =4, column =3, columnspan =1)
     
    txt7.grid(row =6, column =2)
     
    entr1.grid(row =2, column =2)
     
    bou2.grid(row =2, column =3)
    fen1.mainloop()
    bon deja j'ai coloré les canevas en rouge pour mieux les voire, ensuite j'ai utilisé la fonction grid car je la trouve plutot pratique pour organiser la fenetre.
    et voila le resultat obtenue:
    Nom : Sans titre.png
Affichages : 588
Taille : 22,0 Ko
    je vois bien mes 3 canevas mais seulement un seul a la photo.

    est ce que je suis passé à coté de quelque chose?
    merci d'avance pour votre aide.

  2. #2
    Expert confirmé

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 307
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 307
    Par défaut
    Salut,

    Si tu utilises plutôt item1, item2 et item3, c'est pareil ?

  3. #3
    Membre averti
    Homme Profil pro
    ingé automaticien
    Inscrit en
    Juillet 2011
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : ingé automaticien
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2011
    Messages : 37
    Par défaut
    ca me fait exactement la même chose

    et je ne comprend pas pourquoi, les canevas sont indépendants donc il ne devrait pas y avoir de problème. Au début je pensais que la dernière photo écrasé les autre car elle était trop grande. Du coup je les ai redimensionné, mais non .
    Je cherche sur le net mais je ne trouve rien.

    Citation Envoyé par VinsS Voir le message
    Salut,

    Si tu utilises plutôt item1, item2 et item3, c'est pareil ?

  4. #4
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 741
    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 741
    Par défaut
    Citation Envoyé par VinsS Voir le message
    Si tu utilises plutôt item1, item2 et item3, c'est pareil ?
    C'est plutôt photo1, photo2, photo3, sinon on déférence l'instance de PhotoImage.

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

  5. #5
    Membre averti
    Homme Profil pro
    ingé automaticien
    Inscrit en
    Juillet 2011
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : ingé automaticien
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2011
    Messages : 37
    Par défaut

    fait chier le probleme est toujours bete lol merci ca fonctionne :p
    a moins que c'est parceque je faits des erreurs de débutants :p lol

    Citation Envoyé par wiztricks Voir le message
    C'est plutôt photo1, photo2, photo3, sinon on déférence l'instance de PhotoImage.

    - W
    Je viens d'apporter une petite modif pour que le nom du fichier de la photo soit dynamique en fonction du random et la je rencontre un nouveau probleme.
    voila ma modif:
    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
            photod = d+".gif"
            print (photod)
            can1 = Canvas(fen1, width =80, height =80, bg ='red')
            photo1 = PhotoImage(file = photod)
            item = can1.create_image(40, 40, image =photo1)
     
            can1.grid(row =4, column =1, columnspan =1)
     
            photoe = e+".gif"
            print (photoe)
            can2 = Canvas(fen1, width =80, height =80, bg ='red')
            photo2 = PhotoImage(file =photoe)
            item = can2.create_image(40, 40, image =photo2)
     
            can2.grid(row =4, column =2, columnspan =1)
     
            photof = f+".gif"
            print (photof)        
            can3 = Canvas(fen1, width =80, height =80, bg ='red')
            photo3 = PhotoImage(file =photof)
            item = can3.create_image(40, 40, image =photo3)
     
            can3.grid(row =4, column =3, columnspan =1)
    les variables d, e et f prennent le texte qui va bien, c'est à dire banane, pomme, poire, bar, cerise et ananas.
    je voudrais maintenant que le changement de l'image soit dynamique en fonction du tirage aléatoire. je pensais que faire une simple concatenation allé suffir mais visiblement non.
    le print affiche bien le resultat recherché.
    aprés la modif je vois bien les 3 canevas rouge mais rien ne s'affiche dedant. et si par exemple j'ecrits:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
            photof = f+" .gif"
            print (photof)        
            can3 = Canvas(fen1, width =80, height =80, bg ='red')
            photo3 = PhotoImage(file =photof)
            item = can3.create_image(40, 40, image =photo3)
    il me dit que l'image correspondante est introuvable car le nom des photos n'ont pas d'espace.
    a tous les coups c'est encore une connerie.

  6. #6
    Membre averti
    Homme Profil pro
    ingé automaticien
    Inscrit en
    Juillet 2011
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : ingé automaticien
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2011
    Messages : 37
    Par défaut
    je viens de symplifier mon code mais rien n'y fait:
    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
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
     
     
    def alea():
    	import random
    	r = random.randint(1,6)
    	return r
     
    def info(x):
            if x==1 :
                    te="banane.gif"
            if x==2 :
                    te="pomme.gif"
            if x==3 :
                    te="poire.gif"
            if x==4 :
                    te="BAR.gif"
            if x==5 :
                    te="cerise.gif"
            if x==6 :
                    te="ananas.gif"     
            return te
     
    def gain(un,de,tr,gain,somme):
            if un==1 and de==1 and tr==1:
                    mul=2
            elif un==1 and de==1:
                    mul=1.1
            elif un==1 and tr==1:
                    mul=1.1
            elif de==1 and tr==1:
                    mul=1.1
     
            elif un==2 and de==2 and tr==2:
                    mul=3
            elif un==2 and de==2:
                    mul=1.2
            elif un==2 and tr==2:
                    mul=1.2
            elif de==2 and tr==2:
                    mul=1.2
     
            elif un==3 and de==3 and tr==3:
                    mul=4
            elif un==3 and de==3:
                    mul=1.3
            elif un==3 and tr==3:
                    mul=1.3
            elif de==3 and tr==3:
                    mul=1.3
     
            elif un==4 and de==4 and tr==4:
                    mul=5
            elif un==4 and de==4:
                    mul=1.4
            elif un==4 and tr==4:
                    mul=1.4
            elif de==4 and tr==4:
                    mul=1.4
     
            elif un==5 and de==5 and tr==5:
                    mul=6
            elif un==5 and de==5:
                    mul=1.5
            elif un==5 and tr==5:
                    mul=1.5
            elif de==5 and tr==5:
                    mul=1.5
     
            elif un==6 and de==6 and tr==6:
                    mul=7
            elif un==6 and de==6:
                    mul=1.6
            elif un==6 and tr==6:
                    mul=1.6
            elif de==6 and tr==6:
                    mul=1.6           
            else :
                    mul=-1
     
            somme=int(somme+gain*mul)
     
            return somme        
     
    def start_it():
     
            "démarrage de l'animation"
            global argent
            global reste
            global d
            pari =int(entr1.get())
     
            a=alea()
            b=alea()
            c=alea()
     
            d=info(int(a))
            e=info(int(b))
            f=info(int(c))
     
     
            txt4.configure(text = d)
            txt5.configure(text = e)
            txt6.configure(text = f)
     
            argent = gain(a,b,c,pari,argent)
     
     
     
            can1 = Canvas(fen1, width =80, height =80, bg ='red')
            photo1 = PhotoImage(file = d)
            item = can1.create_image(40, 40, image =photo1)
     
            can1.grid(row =4, column =1, columnspan =1)
     
     
            can2 = Canvas(fen1, width =80, height =80, bg ='red')
            photo2 = PhotoImage(file =e)
            item = can2.create_image(40, 40, image =photo2)
     
            can2.grid(row =4, column =2, columnspan =1)
     
            can3 = Canvas(fen1, width =80, height =80, bg ='red')
            photo3 = PhotoImage(file =f)
            item = can3.create_image(40, 40, image =photo3)
     
            can3.grid(row =4, column =3, columnspan =1)
     
            txt7.configure(text = 'il vous argent '+str(argent)+' euros')
     
     
     
    import os
    from tkinter import *
     
    argent = 100
     
     
    pari = 0
    e=d=f="pomme.gif"
     
     
    toto = 'il vous argent ',argent,' euros'
    fen1 = Tk()
    # création de widgets 'Label' et 'Entry' :
    txt1 = Label(fen1, text ='bienvenue, vous avez')
    txt2 = Label(fen1, text =argent)
    txt3 = Label(fen1, text ='euro pour jouer')
    txt4 = Label(fen1, text =e)
    txt5 = Label(fen1, text =d)
    txt6 = Label(fen1, text =f)
    txt7 = Label(fen1, text =toto)
    entr1 = Entry(fen1)
     
     
    bou2 = Button(fen1, text='Démarrer', width =8, command=start_it)
    bou2.pack()
     
     
    txt1.grid(row =1, column =1)
    txt2.grid(row =1, column =2)
    txt3.grid(row =1, column =3)
     
    txt4.grid(row =3, column =1)
    txt5.grid(row =3, column =2)
    txt6.grid(row =3, column =3)
     
     
     
    txt7.grid(row =6, column =2)
     
    entr1.grid(row =2, column =2)
     
    bou2.grid(row =2, column =3)
    fen1.mainloop()
    cette fois ci je fait directement passer le lien par la fonction
    du coup le nom du fichier je le fait passer en paramètre afin de le rendre dynamique.
    mais même résultat les canevas restent vide.

  7. #7
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 741
    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 741
    Par défaut
    Salut,

    Quand j'ecris:

    Citation Envoyé par wiztricks Voir le message
    C'est plutôt photo1, photo2, photo3, sinon on déférence l'instance de PhotoImage.
    çà veut aussi dire que photo1, photo2, photo3 doivent êtes "globales" pour survivre après la sortie de la fonction.

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

  8. #8
    Membre averti
    Homme Profil pro
    ingé automaticien
    Inscrit en
    Juillet 2011
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : ingé automaticien
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2011
    Messages : 37
    Par défaut
    ha pitain carement ............
    bon bein pas besoin de dire ca ca fonctionne tu doit t'en douter .
    et dire que la reponse est la meme que au debut.

    bein un grand merci pour votre aide qui me permet d'avancer.

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

Discussions similaires

  1. Insertion de plusieurs graphiques dans une même feuille
    Par cmoicv dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 19/03/2008, 22h22
  2. [vba-e] insertions de plusieurs cellules dans une
    Par titeZ dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 12/04/2007, 17h14
  3. Insertion d'une photo dans un cadre d'objet dépendant
    Par Rock'Caliente dans le forum Access
    Réponses: 2
    Dernier message: 05/12/2006, 13h15
  4. plusieurs photos dans un état
    Par Anthony17 dans le forum IHM
    Réponses: 5
    Dernier message: 26/07/2006, 17h17

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