Problème python(anaconda)

Fermé
ilhem - Modifié le 16 avril 2019 à 23:22
quent217 Messages postés 421 Date d'inscription vendredi 25 septembre 2015 Statut Membre Dernière intervention 1 mars 2024 - 16 avril 2019 à 23:20
Bonjour,


j'ai un problème d'affichage des images dans une interface graphique en python (anaconda)

from tkinter import * # pour Python2 se serait Tkinter
from tkinter.filedialog import *
from PIL import ImageTk,Image
# Construction de la fenêtre principale «root»
root = Tk()
root.title('Simple exemple')
root.geometry("1350x700")

def alert():
    showinfo("alerte", "Bravo!")

menubar = Menu(root)

menu1 = Menu(menubar, tearoff=0)
menu1.add_command(label="Créer", command=alert)
menu1.add_command(label="Editer", command=alert)
menu1.add_separator()
menu1.add_command(label="Quitter", command=root.quit)
menubar.add_cascade(label="Fichier", menu=menu1)

menu2 = Menu(menubar, tearoff=0)
menu2.add_command(label="Couper", command=alert)
menu2.add_command(label="Copier", command=alert)
menu2.add_command(label="Coller", command=alert)
menubar.add_cascade(label="Editer", menu=menu2)

menu3 = Menu(menubar, tearoff=0)
menu3.add_command(label="A propos", command=alert)
menubar.add_cascade(label="Aide", menu=menu3)

menu4 = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Info", menu=menu4) 

menu5 = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Histogramme", menu=menu5)

menu6 = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Texture", menu=menu6)

root.config(menu=menubar)


lab=LabelFrame(root, text="Indexation", bd=3,width=550,height=300)
#lab.pack()
lab.place(x=10,y=10)
#Canvas(lab, width=150, height=100, bg='ivory').place(x=20,y=20).pack(side=TOP, padx=5, pady=5)
canvas = Canvas(lab, width=200, height=120, bg="yellow",bd=4).place(x=60,y=40)
def action():
    filepath = askopenfilename(title="Ouvrir une image",filetypes=[('jpg files','.jpg'),('all files','.*')])
    print(filepath)
    photo = PhotoImage(file=filepath)
    print(photo)
    im=Image.open(filepath)
    print(photo.width())
    #print('immm',im)
    #canvas = Canvas(lab,width=130, height=120,bd=3,relief=SUNKEN, bg="yellow")
    canvas = Canvas(lab, width=photo.width(), height=photo.height(),bd=4,relief=SUNKEN, bg="yellow")
    canvas.place(x=120,y=50)
    canvas.image=ImageTk.PhotoImage(photo)
    canvas.image.show()
    canvas.create_image(30,20, image=photo)#,anchor='ne')
    root.mainloop()
    #filepath = askopenfilename(title="Ouvrir une image",filetypes=[('png files','.png'),('all files','.*')])
   # photo = PhotoImage(file=filepath)
    
    #canvas.create_image(120, 80, anchor=NW, image=photo)
    root.mainloop()
    #Canvas(lab, width=150, height=100, bg='ivory').place(x=20,y=20)
    #canvas.pack()
  

Button(lab, text ='Recherche image',command=action).place(x=250,y=200)
lab1=LabelFrame(root, text="Indexation1", bd=3,width=550,height=380)
#lab1.pack()
lab1.place(x=10,y=320)

# Histogramme

Histogramme = Frame(lab1, borderwidth=3, relief=GROOVE)
Histogramme.pack(side=LEFT, padx=50, pady=50)


L1 = Label(Histogramme, text="nb_couleurs")
L1.pack( side = BOTTOM)
E1 = Entry(Histogramme, bd =5)
E1.pack(side = BOTTOM)

CheckVar1 = IntVar()
CheckVar2 = IntVar()
C1 = Checkbutton(Histogramme, text = "Histo_couleur", variable = CheckVar1, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C2 = Checkbutton(Histogramme, text = "Histo_n_gris", variable = CheckVar2, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C1.pack(side=BOTTOM)
C2.pack(side=BOTTOM)
Label(Histogramme, text="Histogramme").pack(padx=10, pady=10,side =TOP)
lab2=LabelFrame(root, text="Indexation2", bd=3,width=750,height=690)

#Texture


Texture = Frame(lab1, borderwidth=3,relief=GROOVE)
Texture.pack(side= LEFT, padx=50, pady=50)

L1 = Label(Texture, text="nb_niv_gris")
L1.pack( side = BOTTOM)
E1 = Entry(Texture, bd =5)
E1.pack(side =BOTTOM)

CheckVar1 = IntVar()
CheckVar2 = IntVar()
C3 = Checkbutton(Texture, text = "Image couleur", variable = CheckVar1, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C4 = Checkbutton(Texture, text = "Image niveau de gris", variable = CheckVar2, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C3.pack(side=BOTTOM)
C4.pack(side=BOTTOM)



L1 = Label(Texture, text="Distance")
L1.pack( side = LEFT)
E1 = Entry(Texture, bd =5)
E1.pack(side =BOTTOM )
Label(Texture, text="Texture").pack(padx=10, pady=10,side =TOP)

#lab2.pack()
lab2.place(x=570,y=10)
#a=200
#i=1

#for r in range(3):
 #  for c in range(3):

  #       photo= PhotoImage(file="C:\\Users\\ASUS\\Desktop\\projet_mariem_medicale\\PFE Cyto\\ATRO"+str(i)+".jpg")
         #photo = PhotoImage(file=".//ph2.png")#".//ph"+str(i)+".png")
   #      i=i+1
    #     canvas = Canvas(lab2,width=70, height=70,bd=3,relief=SUNKEN, bg="red")#, width=photo.width(), height=photo.height(),bd=4,relief=SUNKEN, bg="yellow")
       
  #canvas.place(x=a,y=50)
         # a=a+50
#canvas.image=ImageTk.PhotoImage(photo)
    #canvas.image.show()
     #    canvas.create_image(30,30,image=photo)
         #canvas.pack()
      #   canvas.grid(row=r,column=c,ipadx=50, ipady=20)#,padx=10, pady=5)
        #canvas.place(x=650,y=10)
         

 





#for r in range(3):
 #  for c in range(3):
      
         #Button(lab2, text='R%s/C%s'%(r,c),borderwidth=5 ).grid(row=r,column=c,ipadx=80, ipady=20,padx=20, pady=10)
  #       photo= PhotoImage(file="C:\\Users\\asus\\Desktop\\projet_mariem_medicale\\ph"+str(i)+".png")
         #photo = PhotoImage(file=".//ph2.png")#".//ph"+str(i)+".png")
         #photo=Image.open("ph"+str(i)+".png")
   #      i=i+1
         #print(photo)
    #im=Image.open(filepath)
         #print(photo.width())
    #print('immm',im)
    #     l=Label(lab2, image=photo,borderwidth=4 ).grid(row=r,column=c,ipadx=50,ipady=20,padx=10,pady=5)
     #    lab2.mainloop()
        

root.mainloop(  )



EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ici : ICI

Merci d'y penser dans tes prochains messages.

1 réponse

quent217 Messages postés 421 Date d'inscription vendredi 25 septembre 2015 Statut Membre Dernière intervention 1 mars 2024 344
16 avril 2019 à 23:20
Bonjour,
je n'ai pas le courage de comprendre la totalité de votre code pour comprendre quel est l'erreur. Pourriez-vous expliquer ce que vous souhaitez faire et ce qui ne fonctionne pas ? (car un problème d'affichage, c'est un peu flou)
Pouvez-vous aussi poster votre code en utilisant les balises de code ? (explications disponibles ici : https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code) car cela permet de concerver les indentations.
0