Bonjour,
je fais face à un certain nombre de difficultés pour afficher une image jpg avec :
1. la possibilité d'en étendre l'affichage quand on étend le cadre
2. la forme des barres de scrolling
Pour l'instant je bloque sur le problème suivant : ce programme fonctionne correctement (extension et barres)
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
#! python3
# coding: utf-8
 
'''
afficher des images
'''
 
import os
from tkinter import * 
from PIL import Image, ImageTk
 
racine = '/mnt/0ea0b27f-3be6-4d2c-b157-a62559e56142/Photos/Paris/2022/mars 2022/'
 
 
if __name__ == "__main__": # création du main
    global images
    images = os.listdir(racine)
    image_courante = 0
    fenetre = Tk() 
    ara = ImageTk.PhotoImage(Image.open(racine+images[image_courante]))
    cadre = Frame(fenetre)
    cadre.rowconfigure(0, weight=1)
    cadre.columnconfigure(0, weight=1)
    cadre.pack(expand=1, fill=BOTH)
    canvas = Canvas(cadre)
    canvas.grid(row=0, column=0, sticky=N+S+E+W)
    canvas.create_image(0, 0, image=ara)
    scroll_x = Scrollbar(cadre, orient=HORIZONTAL, command=canvas.xview)
    scroll_x.grid(row=1, column=0, sticky=E+W)
    scroll_y = Scrollbar(cadre, orient=VERTICAL, command=canvas.yview)
    scroll_y.grid(row=0, column=1, sticky=N+S)
    canvas["xscrollcommand"] = scroll_x.set
    canvas["yscrollcommand"] = scroll_y.set
    canvas["scrollregion"] = canvas.bbox(ALL)
    bouton = Button(fenetre, text="Quitter", command=fenetre.quit)
    bouton.pack()
    mainloop()
Par contre, si je déplace le code GUI dans une fonction, il n'y a plus d'affichage du contenu, le cadre et les barres étant affichées correctement :
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
#! python3
# coding: utf-8
 
'''
afficher des images
'''
 
import os
from tkinter import * 
from PIL import Image, ImageTk
 
racine = '/mnt/0ea0b27f-3be6-4d2c-b157-a62559e56142/Photos/Paris/2022/mars 2022/'
 
 
def fenetrage(): 
    fenetre = Tk() 
    ara = ImageTk.PhotoImage(Image.open(racine+images[image_courante]))
    cadre = Frame(fenetre)
    cadre.rowconfigure(0, weight=1)
    cadre.columnconfigure(0, weight=1)
    cadre.pack(expand=1, fill=BOTH)
    canvas = Canvas(cadre)
    canvas.grid(row=0, column=0, sticky=N+S+E+W)
    canvas.create_image(0, 0, image=ara)
    scroll_x = Scrollbar(cadre, orient=HORIZONTAL, command=canvas.xview)
    scroll_x.grid(row=1, column=0, sticky=E+W)
    scroll_y = Scrollbar(cadre, orient=VERTICAL, command=canvas.yview)
    scroll_y.grid(row=0, column=1, sticky=N+S)
    canvas["xscrollcommand"] = scroll_x.set
    canvas["yscrollcommand"] = scroll_y.set
    canvas["scrollregion"] = canvas.bbox(ALL)
    bouton = Button(fenetre, text="Quitter", command=fenetre.quit)
    bouton.pack()
 
 
 
if __name__ == "__main__": # création du main
    global images
    images = os.listdir(racine)
    image_courante = 0
 
    fenetrage()
    mainloop()
Merci pour votre aide